<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。">
<meta property="og:type" content="article">
<meta property="og:title" content="力扣周赛283-300">
<meta property="og:url" content="https://hxy1997.xyz/2022/05/15/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B283-300/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515220946.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084434.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084747.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084058.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084114.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221018.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517081713.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517081733.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517083332.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517083423.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221202.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516224907.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516224948.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221234.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516223459.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515212952.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515205618.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515163243.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203205.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203156.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203532.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515212734.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160454.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515151456.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515154208.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515154215.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160205.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515155724.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160233.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515121603.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522144035.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522150904.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522151350.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522151412.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529142047.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529143135.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529143234.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220605164056.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220605164419.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220612180756.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220612181238.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619175907.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619194059.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619194110.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090612.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090449.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090510.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628091142.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705085752.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705085814.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705090111.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706083908.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706083914.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706084510.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706084538.jpeg">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706085534.png">
<meta property="article:published_time" content="2022-05-15T04:00:00.000Z">
<meta property="article:modified_time" content="2022-07-10T10:12:49.956Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="刷题">
<meta property="article:tag" content="力扣">
<meta property="article:tag" content="周赛">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515220946.png">

<link rel="canonical" href="https://hxy1997.xyz/2022/05/15/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B283-300/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>力扣周赛283-300 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2022/05/15/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B283-300/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          力扣周赛283-300
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-05-15 12:00:00" itemprop="dateCreated datePublished" datetime="2022-05-15T12:00:00+08:00">2022-05-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-07-10 18:12:49" itemprop="dateModified" datetime="2022-07-10T18:12:49+08:00">2022-07-10</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%88%B7%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">刷题</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/05/15/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B283-300/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/05/15/%E5%8A%9B%E6%89%A3%E5%91%A8%E8%B5%9B283-300/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>在今年寒假改完论文后，做了一个决定，就是在条件允许的情况下参加力扣周赛。只是做自己想做的事，语言用回C++吧。希望自己能够在其中得到成长。</p>
<span id="more"></span>

<h2 id="Weekly-Contest-283"><a href="#Weekly-Contest-283" class="headerlink" title="Weekly Contest 283"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-283/">Weekly Contest 283</a></h2><p>第一次练手没想到超水平发挥了</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515220946.png" alt="image-20220515220946082"></p>
<h3 id="2194-Cells-in-a-Range-on-an-Excel-Sheet"><a href="#2194-Cells-in-a-Range-on-an-Excel-Sheet" class="headerlink" title="2194. Cells in a Range on an Excel Sheet"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/cells-in-a-range-on-an-excel-sheet/">2194. Cells in a Range on an Excel Sheet</a></h3><p>A cell <code>(r, c)</code> of an excel sheet is represented as a string <code>&quot;&lt;col&gt;&lt;row&gt;&quot;</code> where</p>
<ul>
<li><p>“<col>“denotes the column number:”c”of the cell. It is represented by alphabetical letters</p>
<ul>
<li>For example, the <code>1st</code> column is denoted by <code>&#39;A&#39;</code>, the <code>2nd</code> by <code>&#39;B&#39;</code>, the <code>3rd</code> by <code>&#39;C&#39;</code>, and so on.</li>
</ul>
</li>
<li><p><code>&lt;row&gt;</code> is the row number <code>r</code> of the cell. The <code>rth</code> row is represented by the <strong>integer</strong> <code>r</code>.</p>
</li>
</ul>
<p>You are given a string <code>s</code> in the format <code>&quot;&lt;col1&gt;&lt;row1&gt;:&lt;col2&gt;&lt;row2&gt;&quot;</code>, where <code>&lt;col1&gt;</code> represents the column <code>c1</code>, <code>&lt;row1&gt;</code> represents the row <code>r1</code>, <code>&lt;col2&gt;</code> represents the column <code>c2</code>, and <code>&lt;row2&gt;</code> represents the row <code>r2</code>, such that <code>r1 &lt;= r2</code> and <code>c1 &lt;= c2</code>.</p>
<p>Return <em>the <strong>list of cells</strong></em> <code>(x, y)</code> <em>such that</em> <code>r1 &lt;= x &lt;= r2</code> <em>and</em> <code>c1 &lt;= y &lt;= c2</code>. The cells should be represented as <strong>strings</strong> in the format mentioned above and be sorted in <strong>non-decreasing</strong> order first by columns and then by rows.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084434.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;K1:L2&quot;</span><br><span class="line">Output: [&quot;K1&quot;,&quot;K2&quot;,&quot;L1&quot;,&quot;L2&quot;]</span><br><span class="line">Explanation:</span><br><span class="line">The above diagram shows the cells which should be present in the list.</span><br><span class="line">The red arrows denote the order in which the cells should be presented.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084747.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;A1:F1&quot;</span><br><span class="line">Output: [&quot;A1&quot;,&quot;B1&quot;,&quot;C1&quot;,&quot;D1&quot;,&quot;E1&quot;,&quot;F1&quot;]</span><br><span class="line">Explanation:</span><br><span class="line">The above diagram shows the cells which should be present in the list.</span><br><span class="line">The red arrow denotes the order in which the cells should be presented.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>s.length == 5</code></li>
<li><code>&#39;A&#39; &lt;= s[0] &lt;= s[3] &lt;= &#39;Z&#39;</code></li>
<li><code>&#39;1&#39; &lt;= s[1] &lt;= s[4] &lt;= &#39;9&#39;</code></li>
<li><code>s</code> consists of uppercase English letters, digits and <code>&#39;:&#39;</code>.</li>
</ul>
<p>Easy，转化为行列二重循环</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; <span class="title">cellsInRange</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">char</span> start_letter = s[<span class="number">0</span>], end_letter = s[<span class="number">3</span>];</span><br><span class="line">        <span class="keyword">char</span> start_number = s[<span class="number">1</span>], end_number = s[<span class="number">4</span>];</span><br><span class="line">        <span class="built_in">std</span>::<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c = start_letter; c &lt;= end_letter; c++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">char</span> n = start_number; n &lt;= end_number; n++) &#123;</span><br><span class="line">                <span class="function"><span class="built_in">string</span> <span class="title">y</span><span class="params">(<span class="string">&quot;&quot;</span>)</span></span>;</span><br><span class="line">                y += c;</span><br><span class="line">                y += n;</span><br><span class="line">                res.push_back(y);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2195-Append-K-Integers-With-Minimal-Sum"><a href="#2195-Append-K-Integers-With-Minimal-Sum" class="headerlink" title="2195. Append K Integers With Minimal Sum"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/append-k-integers-with-minimal-sum/">2195. Append K Integers With Minimal Sum</a></h3><p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Append <code>k</code> <strong>unique positive</strong> integers that do <strong>not</strong> appear in <code>nums</code> to <code>nums</code> such that the resulting total sum is <strong>minimum</strong>.</p>
<p>Return <em>the sum of the</em> <code>k</code> <em>integers appended to</em> <code>nums</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,4,25,10,25], k &#x3D; 2</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3.</span><br><span class="line">The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 &#x3D; 70, which is the minimum.</span><br><span class="line">The sum of the two integers appended is 2 + 3 &#x3D; 5, so we return 5.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [5,6], k &#x3D; 6</span><br><span class="line">Output: 25</span><br><span class="line">Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.</span><br><span class="line">The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 &#x3D; 36, which is the minimum. </span><br><span class="line">The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 &#x3D; 25, so we return 25.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
<li><code>1 &lt;= k &lt;= 108</code></li>
</ul>
<p>Medium，原数组排序，依次计算放入后的最小值</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">minimalKSum</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        sort(nums.begin(), nums.end());</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt; used;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> sum = ((<span class="keyword">long</span> <span class="keyword">long</span>)k * (<span class="keyword">long</span> <span class="keyword">long</span>)(k + <span class="number">1</span>)) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">int</span> temp = k + <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num &lt; temp &amp;&amp; used.find(num) == used.end()) &#123;</span><br><span class="line">                sum -= num;</span><br><span class="line">                sum += temp;</span><br><span class="line">                used.insert(num);</span><br><span class="line">                temp++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2196-Create-Binary-Tree-From-Descriptions"><a href="#Failed-2196-Create-Binary-Tree-From-Descriptions" class="headerlink" title="Failed: 2196. Create Binary Tree From Descriptions"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/create-binary-tree-from-descriptions/">2196. Create Binary Tree From Descriptions</a></h3><p>You are given a 2D integer array <code>descriptions</code> where <code>descriptions[i] = [parenti, childi, isLefti]</code> indicates that <code>parenti</code> is the <strong>parent</strong> of <code>childi</code> in a <strong>binary</strong> tree of <strong>unique</strong> values. Furthermore,</p>
<ul>
<li>If <code>isLefti == 1</code>, then <code>childi</code> is the left child of <code>parenti</code>.</li>
<li>If <code>isLefti == 0</code>, then <code>childi</code> is the right child of <code>parenti</code>.</li>
</ul>
<p>Construct the binary tree described by <code>descriptions</code> and return <em>its <strong>root</strong></em>.</p>
<p>The test cases will be generated such that the binary tree is <strong>valid</strong>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084058.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: descriptions &#x3D; [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]</span><br><span class="line">Output: [50,20,80,15,17,19]</span><br><span class="line">Explanation: The root node is the node with value 50 since it has no parent.</span><br><span class="line">The resulting binary tree is shown in the diagram.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517084114.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: descriptions &#x3D; [[1,2,1],[2,3,0],[3,4,1]]</span><br><span class="line">Output: [1,2,null,null,3,4]</span><br><span class="line">Explanation: The root node is the node with value 1 since it has no parent.</span><br><span class="line">The resulting binary tree is shown in the diagram.</span><br></pre></td></tr></table></figure>



<p><strong>Constraints:</strong></p>
<ul>
<li><p><code>1 &lt;= descriptions.length &lt;= 104</code></p>
</li>
<li><p><code>descriptions[i].length == 3</code></p>
</li>
<li><p><code>1 &lt;= parenti, childi &lt;= 105</code></p>
</li>
<li><p><code>0 &lt;= isLefti &lt;= 1</code></p>
</li>
<li><p>The binary tree described by <code>descriptions</code> is valid.</p>
<p>Medium，图的深度遍历</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * struct TreeNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     TreeNode *left;</span></span><br><span class="line"><span class="comment"> *     TreeNode *right;</span></span><br><span class="line"><span class="comment"> *     TreeNode() : val(0), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function">TreeNode* <span class="title">createBinaryTree</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; descriptions)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, TreeNode*&gt; getNode;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">bool</span>&gt; isChild;                               <span class="comment">//to check if node has parent or not</span></span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;v: descriptions)&#123;</span><br><span class="line">            <span class="keyword">if</span> (getNode.count(v[<span class="number">0</span>]) == <span class="number">0</span>)&#123;</span><br><span class="line">                TreeNode* par = <span class="keyword">new</span> TreeNode(v[<span class="number">0</span>]);</span><br><span class="line">                getNode[v[<span class="number">0</span>]] = par;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (getNode.count(v[<span class="number">1</span>]) == <span class="number">0</span>)&#123;</span><br><span class="line">                TreeNode* child = <span class="keyword">new</span> TreeNode(v[<span class="number">1</span>]);</span><br><span class="line">                getNode[v[<span class="number">1</span>]] = child;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(v[<span class="number">2</span>] == <span class="number">1</span>) getNode[v[<span class="number">0</span>]]-&gt;left = getNode[v[<span class="number">1</span>]];               <span class="comment">//left-child</span></span><br><span class="line">            <span class="keyword">else</span> getNode[v[<span class="number">0</span>]]-&gt;right = getNode[v[<span class="number">1</span>]];                     <span class="comment">//right-child</span></span><br><span class="line">            isChild[v[<span class="number">1</span>]] = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        TreeNode* ans = <span class="literal">NULL</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;v: descriptions)&#123;</span><br><span class="line">            <span class="keyword">if</span> (isChild[v[<span class="number">0</span>]] != <span class="literal">true</span>) &#123;                  <span class="comment">//if node has no parent then this is root node</span></span><br><span class="line">                ans = getNode[v[<span class="number">0</span>]]; </span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2197-Replace-Non-Coprime-Numbers-in-Array"><a href="#2197-Replace-Non-Coprime-Numbers-in-Array" class="headerlink" title="2197. Replace Non-Coprime Numbers in Array"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/replace-non-coprime-numbers-in-array/">2197. Replace Non-Coprime Numbers in Array</a></h3><p>You are given an array of integers <code>nums</code>. Perform the following steps:</p>
<ol>
<li>Find <strong>any</strong> two <strong>adjacent</strong> numbers in <code>nums</code> that are <strong>non-coprime</strong>.</li>
<li>If no such numbers are found, <strong>stop</strong> the process.</li>
<li>Otherwise, delete the two numbers and <strong>replace</strong> them with their <strong>LCM (Least Common Multiple)</strong>.</li>
<li><strong>Repeat</strong> this process as long as you keep finding two adjacent non-coprime numbers.</li>
</ol>
<p>Return <em>the <strong>final</strong> modified array.</em> It can be shown that replacing adjacent non-coprime numbers in <strong>any</strong> arbitrary order will lead to the same result.</p>
<p>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>108</code>.</p>
<p>Two values <code>x</code> and <code>y</code> are <strong>non-coprime</strong> if <code>GCD(x, y) &gt; 1</code> where <code>GCD(x, y)</code> is the <strong>Greatest Common Divisor</strong> of <code>x</code> and <code>y</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [6,4,3,2,7,6,2]</span><br><span class="line">Output: [12,7,6]</span><br><span class="line">Explanation: </span><br><span class="line">- (6, 4) are non-coprime with LCM(6, 4) &#x3D; 12. Now, nums &#x3D; [12,3,2,7,6,2].</span><br><span class="line">- (12, 3) are non-coprime with LCM(12, 3) &#x3D; 12. Now, nums &#x3D; [12,2,7,6,2].</span><br><span class="line">- (12, 2) are non-coprime with LCM(12, 2) &#x3D; 12. Now, nums &#x3D; [12,7,6,2].</span><br><span class="line">- (6, 2) are non-coprime with LCM(6, 2) &#x3D; 6. Now, nums &#x3D; [12,7,6].</span><br><span class="line">There are no more adjacent non-coprime numbers in nums.</span><br><span class="line">Thus, the final modified array is [12,7,6].</span><br><span class="line">Note that there are other ways to obtain the same resultant array.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,2,1,1,3,3,3]</span><br><span class="line">Output: [2,1,1,3]</span><br><span class="line">Explanation: </span><br><span class="line">- (3, 3) are non-coprime with LCM(3, 3) &#x3D; 3. Now, nums &#x3D; [2,2,1,1,3,3].</span><br><span class="line">- (3, 3) are non-coprime with LCM(3, 3) &#x3D; 3. Now, nums &#x3D; [2,2,1,1,3].</span><br><span class="line">- (2, 2) are non-coprime with LCM(2, 2) &#x3D; 2. Now, nums &#x3D; [2,1,1,3].</span><br><span class="line">There are no more adjacent non-coprime numbers in nums.</span><br><span class="line">Thus, the final modified array is [2,1,1,3].</span><br><span class="line">Note that there are other ways to obtain the same resultant array.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 105</code></li>
<li>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>108</code>.</li>
</ul>
<p>Hard，相邻的数求最小公倍数，特别注意1</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">replaceNonCoprimes</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; res;</span><br><span class="line">        res.push_back(nums[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            res.push_back(nums[i]);</span><br><span class="line">            <span class="keyword">while</span> (res.size() &gt; <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">int</span> num2 = res[res.size() - <span class="number">1</span>];</span><br><span class="line">                res.pop_back();</span><br><span class="line">                <span class="keyword">int</span> num1 = res[res.size() - <span class="number">1</span>];</span><br><span class="line">                res.pop_back();</span><br><span class="line">                <span class="keyword">int</span> gcd = countGCD(num1, num2);</span><br><span class="line">                <span class="keyword">if</span> (gcd == <span class="number">1</span>) &#123;</span><br><span class="line">                    res.push_back(num1);</span><br><span class="line">                    res.push_back(num2);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    res.push_back(num1 / gcd * num2);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countGCD</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num1 &lt; num2) &#123;</span><br><span class="line">            <span class="keyword">int</span> temp = num1;</span><br><span class="line">            num1 = num2;</span><br><span class="line">            num2 = temp;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (num2 &gt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> m = num1 % num2;</span><br><span class="line">            <span class="keyword">if</span> (m == <span class="number">0</span>) <span class="keyword">return</span> num2;</span><br><span class="line">            <span class="keyword">if</span> (m == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            num1 = num2;</span><br><span class="line">            num2 = m;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> num2 == <span class="number">1</span> ? num2 : num1;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-284"><a href="#Weekly-Contest-284" class="headerlink" title="Weekly Contest 284"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-284/">Weekly Contest 284</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221018.png"></p>
<h3 id="2200-Find-All-K-Distant-Indices-in-an-Array"><a href="#2200-Find-All-K-Distant-Indices-in-an-Array" class="headerlink" title="2200. Find All K-Distant Indices in an Array"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/">2200. Find All K-Distant Indices in an Array</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>key</code> and <code>k</code>. A <strong>k-distant index</strong> is an index <code>i</code> of <code>nums</code> for which there exists at least one index <code>j</code> such that <code>|i - j| &lt;= k</code> and <code>nums[j] == key</code>.</p>
<p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [3,4,9,1,3,9,5], key &#x3D; 9, k &#x3D; 1</span><br><span class="line">Output: [1,2,3,4,5,6]</span><br><span class="line">Explanation: Here, nums[2] &#x3D;&#x3D; key and nums[5] &#x3D;&#x3D; key.</span><br><span class="line">- For index 0, |0 - 2| &gt; k and |0 - 5| &gt; k, so there is no j where |0 - j| &lt;&#x3D; k and nums[j] &#x3D;&#x3D; key. Thus, 0 is not a k-distant index.</span><br><span class="line">- For index 1, |1 - 2| &lt;&#x3D; k and nums[2] &#x3D;&#x3D; key, so 1 is a k-distant index.</span><br><span class="line">- For index 2, |2 - 2| &lt;&#x3D; k and nums[2] &#x3D;&#x3D; key, so 2 is a k-distant index.</span><br><span class="line">- For index 3, |3 - 2| &lt;&#x3D; k and nums[2] &#x3D;&#x3D; key, so 3 is a k-distant index.</span><br><span class="line">- For index 4, |4 - 5| &lt;&#x3D; k and nums[5] &#x3D;&#x3D; key, so 4 is a k-distant index.</span><br><span class="line">- For index 5, |5 - 5| &lt;&#x3D; k and nums[5] &#x3D;&#x3D; key, so 5 is a k-distant index.</span><br><span class="line">- For index 6, |6 - 5| &lt;&#x3D; k and nums[5] &#x3D;&#x3D; key, so 6 is a k-distant index.</span><br><span class="line">Thus, we return [1,2,3,4,5,6] which is sorted in increasing order. </span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,2,2,2,2], key &#x3D; 2, k &#x3D; 2</span><br><span class="line">Output: [0,1,2,3,4]</span><br><span class="line">Explanation: For all indices i in nums, there exists some index j such that |i - j| &lt;&#x3D; k and nums[j] &#x3D;&#x3D; key, so every index is a k-distant index. </span><br><span class="line">Hence, we return [0,1,2,3,4].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
<li><code>key</code> is an integer from the array <code>nums</code>.</li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>
<p>Easy，顺序遍历找到key的位置，再次遍历得到所有数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">findKDistantIndices</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> key, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; index;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; res;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] == key) &#123;</span><br><span class="line">                index.push_back(i);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (index.size() == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> res;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> end = <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; index.size() &amp;&amp; end &lt; n - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> start = <span class="built_in">std</span>::max(end + <span class="number">1</span>, index[i] - k);</span><br><span class="line">            end = <span class="built_in">std</span>::min(index[i] + k, n - <span class="number">1</span>);</span><br><span class="line">            <span class="keyword">if</span> (start &gt; end) &#123;</span><br><span class="line">                end = start;</span><br><span class="line">                <span class="keyword">continue</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = start; j &lt;= end; j++) &#123;</span><br><span class="line">                res.push_back(j);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2201-Count-Artifacts-That-Can-Be-Extracted"><a href="#2201-Count-Artifacts-That-Can-Be-Extracted" class="headerlink" title="2201. Count Artifacts That Can Be Extracted"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-artifacts-that-can-be-extracted/">2201. Count Artifacts That Can Be Extracted</a></h3><p>There is an <code>n x n</code> <strong>0-indexed</strong> grid with some artifacts buried in it. You are given the integer <code>n</code> and a <strong>0-indexed</strong> 2D integer array <code>artifacts</code> describing the positions of the rectangular artifacts where <code>artifacts[i] = [r1i, c1i, r2i, c2i]</code> denotes that the <code>ith</code> artifact is buried in the subgrid where:</p>
<ul>
<li><code>(r1i, c1i)</code> is the coordinate of the <strong>top-left</strong> cell of the <code>ith</code> artifact and</li>
<li><code>(r2i, c2i)</code> is the coordinate of the <strong>bottom-right</strong> cell of the <code>ith</code> artifact.</li>
</ul>
<p>You will excavate some cells of the grid and remove all the mud from them. If the cell has a part of an artifact buried underneath, it will be uncovered. If all the parts of an artifact are uncovered, you can extract it.</p>
<p>Given a <strong>0-indexed</strong> 2D integer array <code>dig</code> where <code>dig[i] = [ri, ci]</code> indicates that you will excavate the cell <code>(ri, ci)</code>, return <em>the number of artifacts that you can extract</em>.</p>
<p>The test cases are generated such that:</p>
<ul>
<li>No two artifacts overlap.</li>
<li>Each artifact only covers at most <code>4</code> cells.</li>
<li>The entries of <code>dig</code> are unique.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517081713.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 2, artifacts &#x3D; [[0,0,0,0],[0,1,1,1]], dig &#x3D; [[0,0],[0,1]]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: </span><br><span class="line">The different colors represent different artifacts. Excavated cells are labeled with a &#39;D&#39; in the grid.</span><br><span class="line">There is 1 artifact that can be extracted, namely the red artifact.</span><br><span class="line">The blue artifact has one part in cell (1,1) which remains uncovered, so we cannot extract it.</span><br><span class="line">Thus, we return 1.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517081733.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 2, artifacts &#x3D; [[0,0,0,0],[0,1,1,1]], dig &#x3D; [[0,0],[0,1],[1,1]]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: Both the red and blue artifacts have all parts uncovered (labeled with a &#39;D&#39;) and can be extracted, so we return 2. </span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= artifacts.length, dig.length &lt;= min(n2, 105)</code></li>
<li><code>artifacts[i].length == 4</code></li>
<li><code>dig[i].length == 2</code></li>
<li><code>0 &lt;= r1i, c1i, r2i, c2i, ri, ci &lt;= n - 1</code></li>
<li><code>r1i &lt;= r2i</code></li>
<li><code>c1i &lt;= c2i</code></li>
<li>No two artifacts will overlap.</li>
<li>The number of cells covered by an artifact is <strong>at most</strong> <code>4</code>.</li>
<li>The entries of <code>dig</code> are unique.</li>
</ul>
<p>Medium，每块瑕疵标记清除</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">digArtifacts</span><span class="params">(<span class="keyword">int</span> n, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; artifacts, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; dig)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; mat(n, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(n, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; artifacts.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> r = artifacts[i][<span class="number">0</span>]; r &lt;= artifacts[i][<span class="number">2</span>]; r++) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> c = artifacts[i][<span class="number">1</span>]; c &lt;= artifacts[i][<span class="number">3</span>]; c++) &#123;</span><br><span class="line">                    mat[r][c] = i  + <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> pos: dig) &#123;</span><br><span class="line">            mat[pos[<span class="number">0</span>]][pos[<span class="number">1</span>]] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">count</span><span class="params">(artifacts.size(), <span class="literal">true</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (mat[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    count[mat[i][j] - <span class="number">1</span>] = <span class="literal">false</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">bool</span> num: count) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num == <span class="literal">true</span>) res++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2202-Maximize-the-Topmost-Element-After-K-Moves"><a href="#Failed-2202-Maximize-the-Topmost-Element-After-K-Moves" class="headerlink" title="Failed: 2202. Maximize the Topmost Element After K Moves"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximize-the-topmost-element-after-k-moves/">2202. Maximize the Topmost Element After K Moves</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the contents of a <strong>pile</strong>, where <code>nums[0]</code> is the topmost element of the pile.</p>
<p>In one move, you can perform <strong>either</strong> of the following:</p>
<ul>
<li>If the pile is not empty, <strong>remove</strong> the topmost element of the pile.</li>
<li>If there are one or more removed elements, <strong>add</strong> any one of them back onto the pile. This element becomes the new topmost element.</li>
</ul>
<p>You are also given an integer <code>k</code>, which denotes the total number of moves to be made.</p>
<p>Return <em>the <strong>maximum value</strong> of the topmost element of the pile possible after <strong>exactly</strong></em> <code>k</code> <em>moves</em>. In case it is not possible to obtain a non-empty pile after <code>k</code> moves, return <code>-1</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [5,2,2,4,0,6], k &#x3D; 4</span><br><span class="line">Output: 5</span><br><span class="line">Explanation:</span><br><span class="line">One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:</span><br><span class="line">- Step 1: Remove the topmost element &#x3D; 5. The pile becomes [2,2,4,0,6].</span><br><span class="line">- Step 2: Remove the topmost element &#x3D; 2. The pile becomes [2,4,0,6].</span><br><span class="line">- Step 3: Remove the topmost element &#x3D; 2. The pile becomes [4,0,6].</span><br><span class="line">- Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].</span><br><span class="line">Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2], k &#x3D; 1</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: </span><br><span class="line">In the first move, our only option is to pop the topmost element of the pile.</span><br><span class="line">Since it is not possible to obtain a non-empty pile after one move, we return -1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>0 &lt;= nums[i], k &lt;= 109</code></li>
</ul>
<p>Medium，使用大根堆保存最大数，再分类讨论</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumTop</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">0</span> || (n == <span class="number">1</span> &amp;&amp; k % <span class="number">2</span> == <span class="number">1</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (k == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> nums[<span class="number">0</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (k == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> nums[<span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, less&lt;<span class="keyword">int</span>&gt;&gt; pq;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= min(k - <span class="number">1</span>, n - <span class="number">1</span>); i++) &#123;</span><br><span class="line">            pq.push(nums[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (k - <span class="number">1</span> &lt; n - <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (pq.top() == nums[k - <span class="number">1</span>]) &#123;</span><br><span class="line">                pq.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (nums[k] &gt; pq.top()) &#123;</span><br><span class="line">                <span class="keyword">return</span> nums[k];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> pq.top();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2203-Minimum-Weighted-Subgraph-With-the-Required-Paths"><a href="#Failed-2203-Minimum-Weighted-Subgraph-With-the-Required-Paths" class="headerlink" title="Failed: 2203. Minimum Weighted Subgraph With the Required Paths"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-weighted-subgraph-with-the-required-paths/">2203. Minimum Weighted Subgraph With the Required Paths</a></h3><p>You are given an integer <code>n</code> denoting the number of nodes of a <strong>weighted directed</strong> graph. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [fromi, toi, weighti]</code> denotes that there exists a <strong>directed</strong> edge from <code>fromi</code> to <code>toi</code> with weight <code>weighti</code>.</p>
<p>Lastly, you are given three <strong>distinct</strong> integers <code>src1</code>, <code>src2</code>, and <code>dest</code> denoting three distinct nodes of the graph.</p>
<p>Return <em>the <strong>minimum weight</strong> of a subgraph of the graph such that it is <strong>possible</strong> to reach</em> <code>dest</code> <em>from both</em> <code>src1</code> <em>and</em> <code>src2</code> <em>via a set of edges of this subgraph</em>. In case such a subgraph does not exist, return <code>-1</code>.</p>
<p>A <strong>subgraph</strong> is a graph whose vertices and edges are subsets of the original graph. The <strong>weight</strong> of a subgraph is the sum of weights of its constituent edges.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517083332.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 6, edges &#x3D; [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 &#x3D; 0, src2 &#x3D; 1, dest &#x3D; 5</span><br><span class="line">Output: 9</span><br><span class="line">Explanation:</span><br><span class="line">The above figure represents the input graph.</span><br><span class="line">The blue edges represent one of the subgraphs that yield the optimal answer.</span><br><span class="line">Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220517083423.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 3, edges &#x3D; [[0,1,1],[2,1,1]], src1 &#x3D; 0, src2 &#x3D; 1, dest &#x3D; 2</span><br><span class="line">Output: -1</span><br><span class="line">Explanation:</span><br><span class="line">The above figure represents the input graph.</span><br><span class="line">It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= n &lt;= 105</code></li>
<li><code>0 &lt;= edges.length &lt;= 105</code></li>
<li><code>edges[i].length == 3</code></li>
<li><code>0 &lt;= fromi, toi, src1, src2, dest &lt;= n - 1</code></li>
<li><code>fromi != toi</code></li>
<li><code>src1</code>, <code>src2</code>, and <code>dest</code> are pairwise distinct.</li>
<li><code>1 &lt;= weight[i] &lt;= 105</code></li>
</ul>
<p>Hard，图，我暂时没学过，暂且等学完算法导论</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line">    <span class="keyword">using</span> pli = <span class="built_in">pair</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>, <span class="keyword">int</span>&gt;;</span><br><span class="line">    <span class="keyword">int</span> n;</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; e1, e2;</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; v1, v2;</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; <span class="title">dijkstra</span><span class="params">(<span class="keyword">int</span> S, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; &amp;e, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; &amp;v)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; <span class="title">d</span><span class="params">(n)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) d[i] = <span class="number">-1</span>;</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;pli&gt; q;</span><br><span class="line">        q.push(pli(<span class="number">0</span>, S));</span><br><span class="line">        <span class="keyword">while</span> (!q.empty()) &#123;</span><br><span class="line">            pli p = q.top();</span><br><span class="line">            q.pop();</span><br><span class="line">            <span class="keyword">int</span> sn = p.second;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> val = -p.first;</span><br><span class="line">            <span class="keyword">if</span> (d[sn] &gt;= <span class="number">0</span>) <span class="keyword">continue</span>;</span><br><span class="line">            d[sn] = val;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; e[sn].size(); i++) q.push(pli(-val - v[sn][i], e[sn][i]));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> d;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">minimumWeight</span><span class="params">(<span class="keyword">int</span> n, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; edges, <span class="keyword">int</span> src1, <span class="keyword">int</span> src2, <span class="keyword">int</span> dest)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>-&gt;n = n;</span><br><span class="line">        e1 = e2 = v1 = v2 = <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;(n);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;edge : edges) &#123;</span><br><span class="line">            e1[edge[<span class="number">0</span>]].push_back(edge[<span class="number">1</span>]);</span><br><span class="line">            v1[edge[<span class="number">0</span>]].push_back(edge[<span class="number">2</span>]);</span><br><span class="line">            e2[edge[<span class="number">1</span>]].push_back(edge[<span class="number">0</span>]);</span><br><span class="line">            v2[edge[<span class="number">1</span>]].push_back(edge[<span class="number">2</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; d1 = dijkstra(src1, e1, v1);</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; d2 = dijkstra(src2, e1, v1);</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; d3 = dijkstra(dest, e2, v2);</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> ans = <span class="number">1e18</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) <span class="keyword">if</span> (d1[i] &gt;= <span class="number">0</span> &amp;&amp; d2[i] &gt;= <span class="number">0</span> &amp;&amp; d3[i] &gt;= <span class="number">0</span>) ans = min(ans, d1[i] + d2[i] + d3[i]);</span><br><span class="line">        <span class="keyword">return</span> ans &lt; <span class="number">1e18</span> ? ans : <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-285"><a href="#Weekly-Contest-285" class="headerlink" title="Weekly Contest 285"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-285/">Weekly Contest 285</a></h2><p>最惨痛的一次，好难，没想到排名还正常</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221202.png" alt="image-20220515221202759"></p>
<h3 id="2210-Count-Hills-and-Valleys-in-an-Array"><a href="#2210-Count-Hills-and-Valleys-in-an-Array" class="headerlink" title="2210. Count Hills and Valleys in an Array"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-hills-and-valleys-in-an-array/">2210. Count Hills and Valleys in an Array</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. An index <code>i</code> is part of a <strong>hill</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are smaller than <code>nums[i]</code>. Similarly, an index <code>i</code> is part of a <strong>valley</strong> in <code>nums</code> if the closest non-equal neighbors of <code>i</code> are larger than <code>nums[i]</code>. Adjacent indices <code>i</code> and <code>j</code> are part of the <strong>same</strong> hill or valley if <code>nums[i] == nums[j]</code>.</p>
<p>Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on <strong>both</strong> the left and right of the index.</p>
<p>Return <em>the number of hills and valleys in</em> <code>nums</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,4,1,1,6,5]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.</span><br><span class="line">At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 &gt; 2 and 4 &gt; 1, index 1 is a hill. </span><br><span class="line">At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 &lt; 4 and 1 &lt; 6, index 2 is a valley.</span><br><span class="line">At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 &lt; 4 and 1 &lt; 6, index 3 is a valley, but note that it is part of the same valley as index 2.</span><br><span class="line">At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 &gt; 1 and 6 &gt; 5, index 4 is a hill.</span><br><span class="line">At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley. </span><br><span class="line">There are 3 hills and valleys so we return 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [6,6,5,5,4,1]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation:</span><br><span class="line">At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.</span><br><span class="line">At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.</span><br><span class="line">At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 &lt; 6 and 5 &gt; 4, index 2 is neither a hill nor a valley.</span><br><span class="line">At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 &lt; 6 and 5 &gt; 4, index 3 is neither a hill nor a valley.</span><br><span class="line">At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 &lt; 5 and 4 &gt; 1, index 4 is neither a hill nor a valley.</span><br><span class="line">At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.</span><br><span class="line">There are 0 hills and valleys so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>
<p>Easy，使用标志位记录前序数是否增序</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countHillValley</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> isAscend = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] &gt; nums[i - <span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (isAscend == <span class="number">-1</span>) &#123;</span><br><span class="line">                    res++;</span><br><span class="line">                &#125;</span><br><span class="line">                isAscend = <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (nums[i] &lt; nums[i - <span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (isAscend == <span class="number">1</span>) &#123;</span><br><span class="line">                    res++;</span><br><span class="line">                &#125;</span><br><span class="line">                isAscend = <span class="number">-1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2211-Count-Collisions-on-a-Road"><a href="#Failed-2211-Count-Collisions-on-a-Road" class="headerlink" title="Failed: 2211. Count Collisions on a Road"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-collisions-on-a-road/">2211. Count Collisions on a Road</a></h3><p>There are <code>n</code> cars on an infinitely long road. The cars are numbered from <code>0</code> to <code>n - 1</code> from left to right and each car is present at a <strong>unique</strong> point.</p>
<p>You are given a <strong>0-indexed</strong> string <code>directions</code> of length <code>n</code>. <code>directions[i]</code> can be either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;S&#39;</code> denoting whether the <code>ith</code> car is moving towards the <strong>left</strong>, towards the <strong>right</strong>, or <strong>staying</strong> at its current point respectively. Each moving car has the <strong>same speed</strong>.</p>
<p>The number of collisions can be calculated as follows:</p>
<ul>
<li>When two cars moving in <strong>opposite</strong> directions collide with each other, the number of collisions increases by <code>2</code>.</li>
<li>When a moving car collides with a stationary car, the number of collisions increases by <code>1</code>.</li>
</ul>
<p>After a collision, the cars involved can no longer move and will stay at the point where they collided. Other than that, cars cannot change their state or direction of motion.</p>
<p>Return <em>the <strong>total number of collisions</strong> that will happen on the road</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: directions &#x3D; &quot;RLRSLL&quot;</span><br><span class="line">Output: 5</span><br><span class="line">Explanation:</span><br><span class="line">The collisions that will happen on the road are:</span><br><span class="line">- Cars 0 and 1 will collide with each other. Since they are moving in opposite directions, the number of collisions becomes 0 + 2 &#x3D; 2.</span><br><span class="line">- Cars 2 and 3 will collide with each other. Since car 3 is stationary, the number of collisions becomes 2 + 1 &#x3D; 3.</span><br><span class="line">- Cars 3 and 4 will collide with each other. Since car 3 is stationary, the number of collisions becomes 3 + 1 &#x3D; 4.</span><br><span class="line">- Cars 4 and 5 will collide with each other. After car 4 collides with car 3, it will stay at the point of collision and get hit by car 5. The number of collisions becomes 4 + 1 &#x3D; 5.</span><br><span class="line">Thus, the total number of collisions that will happen on the road is 5. </span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: directions &#x3D; &quot;LLRR&quot;</span><br><span class="line">Output: 0</span><br><span class="line">Explanation:</span><br><span class="line">No cars will collide with each other. Thus, the total number of collisions that will happen on the road is 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= directions.length &lt;= 105</code></li>
<li><code>directions[i]</code> is either <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, or <code>&#39;S&#39;</code>.</li>
</ul>
<p>Medium，使用栈，细节比较烦没有写出来</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countCollisions</span><span class="params">(<span class="built_in">string</span> directions)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">stack</span>&lt;<span class="keyword">char</span>&gt; stk;</span><br><span class="line">        stk.push(directions[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; directions.length(); i++) &#123;</span><br><span class="line">            <span class="keyword">char</span> c = directions[i];</span><br><span class="line">            <span class="keyword">if</span> (!stk.empty() &amp;&amp; stk.top() == <span class="string">&#x27;R&#x27;</span> &amp;&amp; c == <span class="string">&#x27;L&#x27;</span>) &#123;</span><br><span class="line">                res += <span class="number">2</span>;</span><br><span class="line">                stk.pop();</span><br><span class="line">                stk.push(<span class="string">&#x27;S&#x27;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span>(!stk.empty() &amp;&amp; stk.top() == <span class="string">&#x27;R&#x27;</span> &amp;&amp; c == <span class="string">&#x27;S&#x27;</span>) &#123;</span><br><span class="line">                res++;</span><br><span class="line">                stk.pop();   <span class="comment">// same here </span></span><br><span class="line">                stk.push(<span class="string">&#x27;S&#x27;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span>(!stk.empty() &amp;&amp; stk.top() == <span class="string">&#x27;S&#x27;</span> &amp;&amp; c == <span class="string">&#x27;L&#x27;</span>) <span class="comment">// third case </span></span><br><span class="line">                res++; </span><br><span class="line">            <span class="keyword">else</span></span><br><span class="line">                stk.push(c); <span class="comment">// if no condition is matched push the current element;</span></span><br><span class="line">        &#125; </span><br><span class="line">        <span class="comment">// handling 4th case </span></span><br><span class="line">        <span class="keyword">while</span>(!stk.empty() &amp;&amp; stk.top() == <span class="string">&#x27;R&#x27;</span>) <span class="comment">// remove the leading R&#x27;s</span></span><br><span class="line">            stk.pop();</span><br><span class="line">        <span class="keyword">while</span>(!stk.empty()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (stk.top() == <span class="string">&#x27;R&#x27;</span>) res++;</span><br><span class="line">            stk.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>也可以简化</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countCollisions</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span>(!s.empty() &amp;&amp; s.back() == <span class="string">&#x27;R&#x27;</span>) &#123;</span><br><span class="line">            s.pop_back();</span><br><span class="line">        &#125;</span><br><span class="line">        reverse(s.begin(), s.end());</span><br><span class="line">        <span class="keyword">while</span>  (!s.empty() &amp;&amp; s.back() == <span class="string">&#x27;L&#x27;</span>) &#123;</span><br><span class="line">            s.pop_back();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> ans = (<span class="keyword">int</span>)s.size();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;i : s) &#123;</span><br><span class="line">            ans -= i==<span class="string">&#x27;S&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2212-Maximum-Points-in-an-Archery-Competition"><a href="#Failed-2212-Maximum-Points-in-an-Archery-Competition" class="headerlink" title="Failed: 2212. Maximum Points in an Archery Competition"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-points-in-an-archery-competition/">2212. Maximum Points in an Archery Competition</a></h3><p>Alice and Bob are opponents in an archery competition. The competition has set the following rules:</p>
<ol>
<li>Alice first shoots <code>numArrows</code> arrows and then Bob shoots <code>numArrows</code> arrows.</li>
<li>The points are then calculated as follows:<ol>
<li>The target has integer scoring sections ranging from <code>0</code> to <code>11</code> <strong>inclusive</strong>.</li>
<li>For <strong>each</strong> section of the target with score <code>k</code> (in between <code>0</code> to <code>11</code>), say Alice and Bob have shot <code>ak</code> and <code>bk</code> arrows on that section respectively. If <code>ak &gt;= bk</code>, then Alice takes <code>k</code> points. If <code>ak &lt; bk</code>, then Bob takes <code>k</code> points.</li>
<li>However, if <code>ak == bk == 0</code>, then <strong>nobody</strong> takes <code>k</code> points.</li>
</ol>
</li>
</ol>
<ul>
<li>For example, if Alice and Bob both shot <code>2</code> arrows on the section with score <code>11</code>, then Alice takes <code>11</code> points. On the other hand, if Alice shot <code>0</code> arrows on the section with score <code>11</code> and Bob shot <code>2</code> arrows on that same section, then Bob takes <code>11</code> points.</li>
</ul>
<p>You are given the integer <code>numArrows</code> and an integer array <code>aliceArrows</code> of size <code>12</code>, which represents the number of arrows Alice shot on each scoring section from <code>0</code> to <code>11</code>. Now, Bob wants to <strong>maximize</strong> the total number of points he can obtain.</p>
<p>Return <em>the array</em> <code>bobArrows</code> <em>which represents the number of arrows Bob shot on <strong>each</strong> scoring section from</em> <code>0</code> <em>to</em> <code>11</code>. The sum of the values in <code>bobArrows</code> should equal <code>numArrows</code>.</p>
<p>If there are multiple ways for Bob to earn the maximum total points, return <strong>any</strong> one of them.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516224907.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: numArrows &#x3D; 9, aliceArrows &#x3D; [1,1,0,1,0,0,2,1,0,1,2,0]</span><br><span class="line">Output: [0,0,0,0,1,1,0,0,1,2,3,1]</span><br><span class="line">Explanation: The table above shows how the competition is scored. </span><br><span class="line">Bob earns a total point of 4 + 5 + 8 + 9 + 10 + 11 &#x3D; 47.</span><br><span class="line">It can be shown that Bob cannot obtain a score higher than 47 points.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516224948.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: numArrows &#x3D; 3, aliceArrows &#x3D; [0,0,1,0,0,0,0,0,0,0,0,2]</span><br><span class="line">Output: [0,0,0,0,0,0,0,0,1,1,1,0]</span><br><span class="line">Explanation: The table above shows how the competition is scored.</span><br><span class="line">Bob earns a total point of 8 + 9 + 10 &#x3D; 27.</span><br><span class="line">It can be shown that Bob cannot obtain a score higher than 27 points.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= numArrows &lt;= 105</code></li>
<li><code>aliceArrows.length == bobArrows.length == 12</code></li>
<li><code>0 &lt;= aliceArrows[i], bobArrows[i] &lt;= numArrows</code></li>
<li><code>sum(aliceArrows[i]) == numArrows</code></li>
</ul>
<p>Medium，回溯</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> maxScore = INT_MIN;</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ans;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">helper</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; bobArrows, <span class="keyword">int</span> index, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; aliceArrows, <span class="keyword">int</span> remainArrows, <span class="keyword">int</span> score)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index &lt; <span class="number">0</span> || remainArrows &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (score &gt; maxScore) &#123;</span><br><span class="line">                ans = bobArrows;</span><br><span class="line">                maxScore = score;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        helper(bobArrows, index - <span class="number">1</span>, aliceArrows, remainArrows, score);</span><br><span class="line">        <span class="keyword">if</span> (remainArrows &gt; aliceArrows[index]) &#123;</span><br><span class="line">            bobArrows[index] = aliceArrows[index] + <span class="number">1</span>;</span><br><span class="line">            remainArrows -= bobArrows[index];</span><br><span class="line">            score += index;</span><br><span class="line">            helper(bobArrows, index - <span class="number">1</span>, aliceArrows, remainArrows, score);</span><br><span class="line">            bobArrows[index] = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">maximumBobPoints</span><span class="params">(<span class="keyword">int</span> numArrows, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; aliceArrows)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">bobArrows</span><span class="params">(<span class="number">12</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        helper(bobArrows, <span class="number">11</span>, aliceArrows, numArrows, <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">int</span> arrowUsed = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; <span class="number">12</span>; i++) &#123;</span><br><span class="line">            arrowUsed += ans[i];</span><br><span class="line">        &#125;</span><br><span class="line">        ans[<span class="number">11</span>] += (numArrows - arrowUsed);</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2213-Longest-Substring-of-One-Repeating-Character"><a href="#Failed-2213-Longest-Substring-of-One-Repeating-Character" class="headerlink" title="Failed: 2213. Longest Substring of One Repeating Character"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-substring-of-one-repeating-character/">Failed: 2213. Longest Substring of One Repeating Character</a></h3><p>You are given a <strong>0-indexed</strong> string <code>s</code>. You are also given a <strong>0-indexed</strong> string <code>queryCharacters</code> of length <code>k</code> and a <strong>0-indexed</strong> array of integer <strong>indices</strong> <code>queryIndices</code> of length <code>k</code>, both of which are used to describe <code>k</code> queries.</p>
<p>The <code>ith</code> query updates the character in <code>s</code> at index <code>queryIndices[i]</code> to the character <code>queryCharacters[i]</code>.</p>
<p>Return <em>an array</em> <code>lengths</code> <em>of length</em> <code>k</code> <em>where</em> <code>lengths[i]</code> <em>is the <strong>length</strong> of the <strong>longest substring</strong> of</em> <code>s</code> <em>consisting of <strong>only one repeating</strong> character <strong>after</strong> the</em> <code>ith</code> <em>query</em> <em>is performed.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;babacc&quot;, queryCharacters &#x3D; &quot;bcb&quot;, queryIndices &#x3D; [1,3,3]</span><br><span class="line">Output: [3,3,4]</span><br><span class="line">Explanation: </span><br><span class="line">- 1st query updates s &#x3D; &quot;bbbacc&quot;. The longest substring consisting of one repeating character is &quot;bbb&quot; with length 3.</span><br><span class="line">- 2nd query updates s &#x3D; &quot;bbbccc&quot;. </span><br><span class="line">  The longest substring consisting of one repeating character can be &quot;bbb&quot; or &quot;ccc&quot; with length 3.</span><br><span class="line">- 3rd query updates s &#x3D; &quot;bbbbcc&quot;. The longest substring consisting of one repeating character is &quot;bbbb&quot; with length 4.</span><br><span class="line">Thus, we return [3,3,4].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abyzz&quot;, queryCharacters &#x3D; &quot;aa&quot;, queryIndices &#x3D; [2,1]</span><br><span class="line">Output: [2,3]</span><br><span class="line">Explanation:</span><br><span class="line">- 1st query updates s &#x3D; &quot;abazz&quot;. The longest substring consisting of one repeating character is &quot;zz&quot; with length 2.</span><br><span class="line">- 2nd query updates s &#x3D; &quot;aaazz&quot;. The longest substring consisting of one repeating character is &quot;aaa&quot; with length 3.</span><br><span class="line">Thus, we return [2,3].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>k == queryCharacters.length == queryIndices.length</code></li>
<li><code>1 &lt;= k &lt;= 105</code></li>
<li><code>queryCharacters</code> consists of lowercase English letters.</li>
<li><code>0 &lt;= queryIndices[i] &lt; s.length</code></li>
</ul>
<p>Hard，线段树，暂时不会，抽空学完那本厚厚的算法导论吧。</p>
<h2 id="Weekly-Contest-286"><a href="#Weekly-Contest-286" class="headerlink" title="Weekly Contest 286"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-286/">Weekly Contest 286</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515221234.png" alt="image-20220515221233910"></p>
<h3 id="2215-Find-the-Difference-of-Two-Arrays"><a href="#2215-Find-the-Difference-of-Two-Arrays" class="headerlink" title="2215. Find the Difference of Two Arrays"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-the-difference-of-two-arrays/">2215. Find the Difference of Two Arrays</a></h3><p>Given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, return <em>a list</em> <code>answer</code> <em>of size</em> <code>2</code> <em>where:</em></p>
<ul>
<li><code>answer[0]</code> <em>is a list of all <strong>distinct</strong> integers in</em> <code>nums1</code> <em>which are <strong>not</strong> present in</em> <code>nums2</code><em>.</em></li>
<li><code>answer[1]</code> <em>is a list of all <strong>distinct</strong> integers in</em> <code>nums2</code> <em>which are <strong>not</strong> present in</em> <code>nums1</code>.</li>
</ul>
<p><strong>Note</strong> that the integers in the lists may be returned in <strong>any</strong> order.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums1 &#x3D; [1,2,3], nums2 &#x3D; [2,4,6]</span><br><span class="line">Output: [[1,3],[4,6]]</span><br><span class="line">Explanation:</span><br><span class="line">For nums1, nums1[1] &#x3D; 2 is present at index 0 of nums2, whereas nums1[0] &#x3D; 1 and nums1[2] &#x3D; 3 are not present in nums2. Therefore, answer[0] &#x3D; [1,3].</span><br><span class="line">For nums2, nums2[0] &#x3D; 2 is present at index 1 of nums1, whereas nums2[1] &#x3D; 4 and nums2[2] &#x3D; 6 are not present in nums2. Therefore, answer[1] &#x3D; [4,6].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: nums1 &#x3D; [1,2,3,3], nums2 &#x3D; [1,1,2,2]</span><br><span class="line">Output: [[3],[]]</span><br><span class="line">Explanation:</span><br><span class="line">For nums1, nums1[2] and nums1[3] are not present in nums2. Since nums1[2] &#x3D;&#x3D; nums1[3], their value is only included once and answer[0] &#x3D; [3].</span><br><span class="line">Every integer in nums2 is present in nums1. Therefore, answer[1] &#x3D; [].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>
<li><code>-1000 &lt;= nums1[i], nums2[i] &lt;= 1000</code></li>
</ul>
<p>Easy，哈希保存数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; findDifference(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums1, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums2) &#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; counted1;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ans1;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ans2;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums1) &#123;</span><br><span class="line">            counted1[num] = <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums2) &#123;</span><br><span class="line">            <span class="keyword">if</span> (counted1[num]) &#123;</span><br><span class="line">                counted1[num] = <span class="number">2</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                counted1[num] = <span class="number">2</span>;</span><br><span class="line">                ans2.push_back(num);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num: nums1) &#123;</span><br><span class="line">            <span class="keyword">if</span> (counted1[num] == <span class="number">1</span>) &#123;</span><br><span class="line">                counted1[num] = <span class="number">2</span>;</span><br><span class="line">                ans1.push_back(num);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> &#123;ans1, ans2&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2216-Minimum-Deletions-to-Make-Array-Beautiful"><a href="#2216-Minimum-Deletions-to-Make-Array-Beautiful" class="headerlink" title="2216. Minimum Deletions to Make Array Beautiful"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-deletions-to-make-array-beautiful/">2216. Minimum Deletions to Make Array Beautiful</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. The array <code>nums</code> is <strong>beautiful</strong> if:</p>
<ul>
<li><code>nums.length</code> is even.</li>
<li><code>nums[i] != nums[i + 1]</code> for all <code>i % 2 == 0</code>.</li>
</ul>
<p>Note that an empty array is considered beautiful.</p>
<p>You can delete any number of elements from <code>nums</code>. When you delete an element, all the elements to the right of the deleted element will be <strong>shifted one unit to the left</strong> to fill the gap created and all the elements to the left of the deleted element will remain <strong>unchanged</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of elements to delete from</em> <code>nums</code> <em>to make it</em> <em>beautiful.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,1,2,3,5]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: You can delete either nums[0] or nums[1] to make nums &#x3D; [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,1,2,2,3,3]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: You can delete nums[0] and nums[5] to make nums &#x3D; [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>0 &lt;= nums[i] &lt;= 105</code></li>
</ul>
<p>Medium，依次遍历，连续相同的数最多保存2个，且满足第一个数的位置为奇数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minDeletion</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (i &lt; nums.size()) &#123;</span><br><span class="line">            <span class="keyword">int</span> j = i;</span><br><span class="line">            <span class="keyword">while</span> (j &lt; nums.size() &amp;&amp; nums[i] == nums[j]) &#123;</span><br><span class="line">                j++;</span><br><span class="line">            &#125;</span><br><span class="line">            len++;</span><br><span class="line">            <span class="keyword">if</span> ((j &gt; i + <span class="number">1</span>) &amp;&amp; (len % <span class="number">2</span> == <span class="number">0</span>)) &#123;</span><br><span class="line">                len++;</span><br><span class="line">            &#125;</span><br><span class="line">            i = j;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (len % <span class="number">2</span> == <span class="number">1</span>) &#123;</span><br><span class="line">            len--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> nums.size() - len;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2217-Find-Palindrome-With-Fixed-Length"><a href="#Failed-2217-Find-Palindrome-With-Fixed-Length" class="headerlink" title="Failed: 2217. Find Palindrome With Fixed Length"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-palindrome-with-fixed-length/">2217. Find Palindrome With Fixed Length</a></h3><p>Given an integer array <code>queries</code> and a <strong>positive</strong> integer <code>intLength</code>, return <em>an array</em> <code>answer</code> <em>where</em> <code>answer[i]</code> <em>is either the</em> <code>queries[i]th</code> <em>smallest <strong>positive palindrome</strong> of length</em> <code>intLength</code> <em>or</em> <code>-1</code> <em>if no such palindrome exists</em>.</p>
<p>A <strong>palindrome</strong> is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: queries &#x3D; [1,2,3,4,5,90], intLength &#x3D; 3</span><br><span class="line">Output: [101,111,121,131,141,999]</span><br><span class="line">Explanation:</span><br><span class="line">The first few palindromes of length 3 are:</span><br><span class="line">101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...</span><br><span class="line">The 90th palindrome of length 3 is 999.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: queries &#x3D; [2,4,6], intLength &#x3D; 4</span><br><span class="line">Output: [1111,1331,1551]</span><br><span class="line">Explanation:</span><br><span class="line">The first six palindromes of length 4 are:</span><br><span class="line">1001, 1111, 1221, 1331, 1441, and 1551.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= queries.length &lt;= 5 * 104</code></li>
<li><code>1 &lt;= queries[i] &lt;= 109</code></li>
<li><code>1 &lt;= intLength &lt;= 15</code></li>
</ul>
<p>Medium，将每个回文数分为前半部分和后半部分，翻折得到所求结果</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; <span class="title">kthPalindrome</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; queries, <span class="keyword">int</span> intLength)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">long</span> <span class="keyword">long</span>&gt; res;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> base = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> half = (intLength + <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; half - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            base *= <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; queries.size(); i++) &#123;</span><br><span class="line">            res.push_back(countPalindrome(queries[i], base, intLength));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">countPalindrome</span><span class="params">(<span class="keyword">int</span> k, <span class="keyword">long</span> <span class="keyword">long</span> base,<span class="keyword">int</span> intLength)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (base + k - <span class="number">1</span> &gt;= base * <span class="number">10</span>) <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        base += (k - <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = base;</span><br><span class="line">        <span class="keyword">if</span> (intLength % <span class="number">2</span> == <span class="number">1</span>) &#123;</span><br><span class="line">            base /= <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> half = intLength / <span class="number">2</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; half; i++) &#123;</span><br><span class="line">            res *= <span class="number">10</span>;</span><br><span class="line">            res += base % <span class="number">10</span>;</span><br><span class="line">            base /= <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2218-Maximum-Value-of-K-Coins-From-Piles"><a href="#Failed-2218-Maximum-Value-of-K-Coins-From-Piles" class="headerlink" title="Failed: 2218. Maximum Value of K Coins From Piles"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/">2218. Maximum Value of K Coins From Piles</a></h3><p>There are <code>n</code> <strong>piles</strong> of coins on a table. Each pile consists of a <strong>positive number</strong> of coins of assorted denominations.</p>
<p>In one move, you can choose any coin on <strong>top</strong> of any pile, remove it, and add it to your wallet.</p>
<p>Given a list <code>piles</code>, where <code>piles[i]</code> is a list of integers denoting the composition of the <code>ith</code> pile from <strong>top to bottom</strong>, and a positive integer <code>k</code>, return <em>the <strong>maximum total value</strong> of coins you can have in your wallet if you choose <strong>exactly</strong></em> <code>k</code> <em>coins optimally</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220516223459.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: piles &#x3D; [[1,100,3],[7,8,9]], k &#x3D; 2</span><br><span class="line">Output: 101</span><br><span class="line">Explanation:</span><br><span class="line">The above diagram shows the different ways we can choose k coins.</span><br><span class="line">The maximum total we can obtain is 101.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: piles &#x3D; [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k &#x3D; 7</span><br><span class="line">Output: 706</span><br><span class="line">Explanation:</span><br><span class="line">The maximum total can be obtained if we choose all coins from the last pile.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == piles.length</code></li>
<li><code>1 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= piles[i][j] &lt;= 105</code></li>
<li><code>1 &lt;= k &lt;= sum(piles[i].length) &lt;= 2000</code></li>
</ul>
<p>Hard，记忆化深度搜索</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maxValueOfCoins</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; piles, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = piles.size();</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; dp(n + <span class="number">1</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(k + <span class="number">1</span>, <span class="number">0</span>));</span><br><span class="line">        function&lt;<span class="keyword">int</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)&gt; dfs = [&amp;](<span class="keyword">int</span> i, <span class="keyword">int</span> k) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dp[i][k] &gt; <span class="number">0</span>) <span class="keyword">return</span> dp[i][k];</span><br><span class="line">            <span class="keyword">if</span> (i == n || k == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">int</span> res = dfs(i + <span class="number">1</span>, k), cur = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; piles[i].size() &amp;&amp; j &lt; k; j++) &#123;</span><br><span class="line">                cur += piles[i][j];</span><br><span class="line">                res = max(res, dfs(i + <span class="number">1</span>, k - j - <span class="number">1</span>) + cur);</span><br><span class="line">            &#125;</span><br><span class="line">            dp[i][k] = res;</span><br><span class="line">            <span class="keyword">return</span> res;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="keyword">return</span> dfs(<span class="number">0</span>, k);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-287"><a href="#Weekly-Contest-287" class="headerlink" title="Weekly Contest 287"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-287/">Weekly Contest 287</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515212952.png" alt="image-20220515212952499"></p>
<h3 id="Failed-2224-Minimum-Number-of-Operations-to-Convert-Time"><a href="#Failed-2224-Minimum-Number-of-Operations-to-Convert-Time" class="headerlink" title="Failed: 2224. Minimum Number of Operations to Convert Time"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-number-of-operations-to-convert-time/">2224. Minimum Number of Operations to Convert Time</a></h3><p>You are given two strings <code>current</code> and <code>correct</code> representing two <strong>24-hour times</strong>.</p>
<p>24-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <code>00</code> and <code>23</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 24-hour time is <code>00:00</code>, and the latest is <code>23:59</code>.</p>
<p>In one operation you can increase the time <code>current</code> by <code>1</code>, <code>5</code>, <code>15</code>, or <code>60</code> minutes. You can perform this operation <strong>any</strong> number of times.</p>
<p>Return <em>the <strong>minimum number of operations</strong> needed to convert</em> <code>current</code> <em>to</em> <code>correct</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: current &#x3D; &quot;02:30&quot;, correct &#x3D; &quot;04:35&quot;</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">We can convert current to correct in 3 operations as follows:</span><br><span class="line">- Add 60 minutes to current. current becomes &quot;03:30&quot;.</span><br><span class="line">- Add 60 minutes to current. current becomes &quot;04:30&quot;.</span><br><span class="line">- Add 5 minutes to current. current becomes &quot;04:35&quot;.</span><br><span class="line">It can be proven that it is not possible to convert current to correct in fewer than 3 operations.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: current &#x3D; &quot;11:00&quot;, correct &#x3D; &quot;11:01&quot;</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: We only have to add one minute to current, so the minimum number of operations needed is 1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>current</code> and <code>correct</code> are in the format <code>&quot;HH:MM&quot;</code></li>
<li><code>current &lt;= correct</code></li>
</ul>
<p>Easy，很简单的题，想复杂了，总有corner case</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">getTime</span><span class="params">(<span class="built_in">string</span> &amp;s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> stoi(s.substr(<span class="number">0</span>, <span class="number">2</span>)) * <span class="number">60</span> + stoi(s.substr(<span class="number">3</span>));</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">convertTime</span><span class="params">(<span class="built_in">string</span> current, <span class="built_in">string</span> correct)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> diff = getTime(correct) - getTime(current), ops[<span class="number">4</span>] = &#123;<span class="number">60</span>, <span class="number">15</span>, <span class="number">5</span>, <span class="number">1</span>&#125;, ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> op : ops) &#123;</span><br><span class="line">            ans += diff / op;</span><br><span class="line">            diff %= op;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2225-Find-Players-With-Zero-or-One-Losses"><a href="#2225-Find-Players-With-Zero-or-One-Losses" class="headerlink" title="2225. Find Players With Zero or One Losses"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-players-with-zero-or-one-losses/">2225. Find Players With Zero or One Losses</a></h3><p>You are given an integer array <code>matches</code> where <code>matches[i] = [winneri, loseri]</code> indicates that the player <code>winneri</code> defeated player <code>loseri</code> in a match.</p>
<p>Return <em>a list</em> <code>answer</code> <em>of size</em> <code>2</code> <em>where:</em></p>
<ul>
<li><code>answer[0]</code> is a list of all players that have <strong>not</strong> lost any matches.</li>
<li><code>answer[1]</code> is a list of all players that have lost exactly <strong>one</strong> match.</li>
</ul>
<p>The values in the two lists should be returned in <strong>increasing</strong> order.</p>
<p><strong>Note:</strong></p>
<ul>
<li>You should only consider the players that have played <strong>at least one</strong> match.</li>
<li>The testcases will be generated such that <strong>no</strong> two matches will have the <strong>same</strong> outcome.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: matches &#x3D; [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]</span><br><span class="line">Output: [[1,2,10],[4,5,7,8]]</span><br><span class="line">Explanation:</span><br><span class="line">Players 1, 2, and 10 have not lost any matches.</span><br><span class="line">Players 4, 5, 7, and 8 each have lost one match.</span><br><span class="line">Players 3, 6, and 9 each have lost two matches.</span><br><span class="line">Thus, answer[0] &#x3D; [1,2,10] and answer[1] &#x3D; [4,5,7,8].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: matches &#x3D; [[2,3],[1,3],[5,4],[6,4]]</span><br><span class="line">Output: [[1,2,5,6],[]]</span><br><span class="line">Explanation:</span><br><span class="line">Players 1, 2, 5, and 6 have not lost any matches.</span><br><span class="line">Players 3 and 4 each have lost two matches.</span><br><span class="line">Thus, answer[0] &#x3D; [1,2,5,6] and answer[1] &#x3D; [].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= matches.length &lt;= 105</code></li>
<li><code>matches[i].length == 2</code></li>
<li><code>1 &lt;= winneri, loseri &lt;= 105</code></li>
<li><code>winneri != loseri</code></li>
<li>All <code>matches[i]</code> are <strong>unique</strong>.</li>
</ul>
<p>Medium，分别记录胜者和负者</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; findWinners(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; matches) &#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; countLoss;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> match: matches) &#123;</span><br><span class="line">            <span class="keyword">if</span> (countLoss[match[<span class="number">1</span>]]) &#123;</span><br><span class="line">                countLoss[match[<span class="number">1</span>]]++;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                 countLoss[match[<span class="number">1</span>]] = <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="keyword">int</span>&gt; vic;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ans1, ans2;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> match: matches) &#123;</span><br><span class="line">            <span class="keyword">if</span> (!countLoss[match[<span class="number">0</span>]]) &#123;</span><br><span class="line">                vic.insert(match[<span class="number">0</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (countLoss[match[<span class="number">1</span>]] == <span class="number">1</span>) &#123;</span><br><span class="line">                ans2.push_back(match[<span class="number">1</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        ans1.insert(ans1.end(), vic.begin(), vic.end());</span><br><span class="line">        sort(ans1.begin(), ans1.end());</span><br><span class="line">        sort(ans2.begin(), ans2.end());</span><br><span class="line">        <span class="keyword">return</span> &#123;ans1, ans2&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2226-Find-Players-With-Zero-or-One-Losses"><a href="#2226-Find-Players-With-Zero-or-One-Losses" class="headerlink" title="2226. Find Players With Zero or One Losses"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-players-with-zero-or-one-losses/">2226. Find Players With Zero or One Losses</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.</p>
<p>You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can take <strong>at most one</strong> pile of candies and some piles of candies may go unused.</p>
<p>Return <em>the <strong>maximum number of candies</strong> each child can get.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: candies &#x3D; [5,8,6], k &#x3D; 3</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: candies &#x3D; [2,5], k &#x3D; 11</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= candies.length &lt;= 105</code></li>
<li><code>1 &lt;= candies[i] &lt;= 107</code></li>
<li><code>1 &lt;= k &lt;= 1012</code></li>
</ul>
<p>Medium，贪心+二分</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumCandies</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; candies, <span class="keyword">long</span> <span class="keyword">long</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> left = <span class="number">1</span>, right = candies[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; candies.size(); i++) &#123;</span><br><span class="line">            right = max(right, candies[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">            <span class="keyword">int</span> mid = left + (right - left) / <span class="number">2</span>;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> count = candies[<span class="number">0</span>] / mid;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; candies.size(); i++) &#123;</span><br><span class="line">                count += candies[i] / mid;</span><br><span class="line">                <span class="keyword">if</span> (count &gt;= k) <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (count &lt; k) &#123;</span><br><span class="line">                right = mid - <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                ans = max(ans, mid);</span><br><span class="line">                left = mid + <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2227-Encrypt-and-Decrypt-Strings"><a href="#Failed-2227-Encrypt-and-Decrypt-Strings" class="headerlink" title="Failed: 2227. Encrypt and Decrypt Strings"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-players-with-zero-or-one-losses/">2227. Encrypt and Decrypt Strings</a></h3><p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p>
<p>A string is <strong>encrypted</strong> with the following process:</p>
<ol>
<li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li>
<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>
</ol>
<p>Note that in case a character of the string is <strong>not present</strong> in <code>keys</code>, the encryption process cannot be carried out, and an empty string <code>&quot;&quot;</code> is returned.</p>
<p>A string is <strong>decrypted</strong> with the following process:</p>
<ol>
<li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li>
<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>
</ol>
<p>Implement the <code>Encrypter</code> class:</p>
<ul>
<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li>
<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>
<li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Input</span><br><span class="line">[&quot;Encrypter&quot;, &quot;encrypt&quot;, &quot;decrypt&quot;]</span><br><span class="line">[[[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]], [&quot;abcd&quot;], [&quot;eizfeiam&quot;]]</span><br><span class="line">Output</span><br><span class="line">[null, &quot;eizfeiam&quot;, 2]</span><br><span class="line"></span><br><span class="line">Explanation</span><br><span class="line">Encrypter encrypter &#x3D; new Encrypter([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]);</span><br><span class="line">encrypter.encrypt(&quot;abcd&quot;); &#x2F;&#x2F; return &quot;eizfeiam&quot;. </span><br><span class="line">                           &#x2F;&#x2F; &#39;a&#39; maps to &quot;ei&quot;, &#39;b&#39; maps to &quot;zf&quot;, &#39;c&#39; maps to &quot;ei&quot;, and &#39;d&#39; maps to &quot;am&quot;.</span><br><span class="line">encrypter.decrypt(&quot;eizfeiam&quot;); &#x2F;&#x2F; return 2. </span><br><span class="line">                              &#x2F;&#x2F; &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;. </span><br><span class="line">                              &#x2F;&#x2F; Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;. </span><br><span class="line">                              &#x2F;&#x2F; 2 of those strings, &quot;abad&quot; and &quot;abcd&quot;, appear in dictionary, so the answer is 2.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li>
<li><code>values[i].length == 2</code></li>
<li><code>1 &lt;= dictionary.length &lt;= 100</code></li>
<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>
<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>
<li><code>1 &lt;= word1.length &lt;= 2000</code></li>
<li><code>1 &lt;= word2.length &lt;= 200</code></li>
<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>
<li><code>word2.length</code> is even.</li>
<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>
<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>
</ul>
<p>Hard，愚人节礼物，保存每个解码即可</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Encrypter</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">array</span>&lt;<span class="built_in">string</span>, 26&gt; mp;</span><br><span class="line">    <span class="built_in">unordered_map</span>&lt;<span class="built_in">string</span>, <span class="keyword">int</span>&gt; cnt;</span><br><span class="line">    Encrypter(<span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt;&amp; keys, <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; values, <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; dictionary) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; keys.size(); i++) &#123;</span><br><span class="line">            mp[keys[i] - <span class="string">&#x27;a&#x27;</span>] = values[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">string</span> word: dictionary) &#123;</span><br><span class="line">            cnt[encrypt(word)]++;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">encrypt</span><span class="params">(<span class="built_in">string</span> word1)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> ch : word1) &#123;</span><br><span class="line">            <span class="keyword">auto</span> &amp;s = mp[ch - <span class="string">&#x27;a&#x27;</span>];</span><br><span class="line">            <span class="keyword">if</span> (s == <span class="string">&quot;&quot;</span>) <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">            res += s;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">decrypt</span><span class="params">(<span class="built_in">string</span> word2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> cnt.count(word2) ? cnt[word2] : <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your Encrypter object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * Encrypter* obj = new Encrypter(keys, values, dictionary);</span></span><br><span class="line"><span class="comment"> * string param_1 = obj-&gt;encrypt(word1);</span></span><br><span class="line"><span class="comment"> * int param_2 = obj-&gt;decrypt(word2);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-288"><a href="#Weekly-Contest-288" class="headerlink" title="Weekly Contest 288"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-288/">Weekly Contest 288</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515205618.png" alt="image-20220515205618228"></p>
<h3 id="2231-Largest-Number-After-Digit-Swaps-by-Parity"><a href="#2231-Largest-Number-After-Digit-Swaps-by-Parity" class="headerlink" title="2231. Largest Number After Digit Swaps by Parity"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/">2231. Largest Number After Digit Swaps by Parity</a></h3><p>You are given a positive integer <code>num</code>. You may swap any two digits of <code>num</code> that have the same <strong>parity</strong> (i.e. both odd digits or both even digits).</p>
<p>Return <em>the <strong>largest</strong> possible value of</em> <code>num</code> <em>after <strong>any</strong> number of swaps.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; 1234</span><br><span class="line">Output: 3412</span><br><span class="line">Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.</span><br><span class="line">Swap the digit 2 with the digit 4, this results in the number 3412.</span><br><span class="line">Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.</span><br><span class="line">Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; 65875</span><br><span class="line">Output: 87655</span><br><span class="line">Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.</span><br><span class="line">Swap the first digit 5 with the digit 7, this results in the number 87655.</span><br><span class="line">Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 109</code></li>
</ul>
<p>Easy，奇偶数位分别排序</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">largestInteger</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; odd, even;</span><br><span class="line">        <span class="keyword">int</span> temp = num;</span><br><span class="line">        <span class="keyword">int</span> base = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (temp &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> cur = temp  % <span class="number">10</span>;</span><br><span class="line">            <span class="keyword">if</span> (cur % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                even.push_back(cur);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                odd.push_back(cur);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (base == <span class="number">0</span>) &#123;</span><br><span class="line">                base = <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                base = base * <span class="number">10</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            temp = temp / <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        sort(odd.begin(), odd.end());</span><br><span class="line">        sort(even.begin(), even.end());</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (base &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            res = res * <span class="number">10</span>;</span><br><span class="line">            <span class="keyword">int</span> cur = num / base;</span><br><span class="line">            <span class="keyword">if</span> (cur % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                res += even.back();</span><br><span class="line">                even.pop_back();</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                res += odd.back();</span><br><span class="line">                odd.pop_back();</span><br><span class="line">            &#125;</span><br><span class="line">            num = num % base;</span><br><span class="line">            base = base / <span class="number">10</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2232-Minimize-Result-by-Adding-Parentheses-to-Expression"><a href="#Failed-2232-Minimize-Result-by-Adding-Parentheses-to-Expression" class="headerlink" title="Failed: 2232. Minimize Result by Adding Parentheses to Expression"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/">2232. Minimize Result by Adding Parentheses to Expression</a></h3><p>You are given a <strong>0-indexed</strong> string <code>expression</code> of the form <code>&quot;&lt;num1&gt;+&lt;num2&gt;&quot;</code> where <code>&lt;num1&gt;</code> and <code>&lt;num2&gt;</code> represent positive integers.</p>
<p>Add a pair of parentheses to <code>expression</code> such that after the addition of parentheses, <code>expression</code> is a <strong>valid</strong> mathematical expression and evaluates to the <strong>smallest</strong> possible value. The left parenthesis <strong>must</strong> be added to the left of <code>&#39;+&#39;</code> and the right parenthesis <strong>must</strong> be added to the right of <code>&#39;+&#39;</code>.</p>
<p>Return <code>expression</code> <em>after adding a pair of parentheses such that</em> <code>expression</code> <em>evaluates to the <strong>smallest</strong> possible value.</em> If there are multiple answers that yield the same result, return any of them.</p>
<p>The input has been generated such that the original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: expression &#x3D; &quot;247+38&quot;</span><br><span class="line">Output: &quot;2(47+38)&quot;</span><br><span class="line">Explanation: The expression evaluates to 2 * (47 + 38) &#x3D; 2 * 85 &#x3D; 170.</span><br><span class="line">Note that &quot;2(4)7+38&quot; is invalid because the right parenthesis must be to the right of the &#39;+&#39;.</span><br><span class="line">It can be shown that 170 is the smallest possible value.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: expression &#x3D; &quot;12+34&quot;</span><br><span class="line">Output: &quot;1(2+3)4&quot;</span><br><span class="line">Explanation: The expression evaluates to 1 * (2 + 3) * 4 &#x3D; 1 * 5 * 4 &#x3D; 20.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: expression &#x3D; &quot;999+999&quot;</span><br><span class="line">Output: &quot;(999+999)&quot;</span><br><span class="line">Explanation: The expression evaluates to 999 + 999 &#x3D; 1998.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= expression.length &lt;= 10</code></li>
<li><code>expression</code> consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code> and <code>&#39;+&#39;</code>.</li>
<li><code>expression</code> starts and ends with digits.</li>
<li><code>expression</code> contains exactly one <code>&#39;+&#39;</code>.</li>
<li>The original value of <code>expression</code>, and the value of <code>expression</code> after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.</li>
</ul>
<p>Medium，遍历各种括号可能位置，找最大值</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">minimizeResult</span><span class="params">(<span class="built_in">string</span> expression)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> index;</span><br><span class="line">        <span class="keyword">int</span> n = expression.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (expression[i]==<span class="string">&#x27;+&#x27;</span>) &#123;</span><br><span class="line">                index = i;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="built_in">string</span> num1 = expression.substr(<span class="number">0</span>, index);</span><br><span class="line">        <span class="built_in">string</span> num2 = expression.substr(index + <span class="number">1</span>, n - index - <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">int</span> p1, p2;</span><br><span class="line">        <span class="keyword">int</span> min_res = INT_MAX;</span><br><span class="line">        <span class="built_in">string</span> ans;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> b1 = <span class="number">0</span>; b1 &lt; num1.size(); b1++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> b2 = <span class="number">0</span>; b2 &lt; num2.size(); b2++) &#123;</span><br><span class="line">                <span class="built_in">string</span> s1 = num1.substr(<span class="number">0</span>, b1);</span><br><span class="line">                <span class="built_in">string</span> s2 = num2.substr(b2 + <span class="number">1</span>, num2.size() - b2 + <span class="number">1</span>);</span><br><span class="line">                <span class="keyword">if</span> (s1.empty()) &#123;</span><br><span class="line">                    p1 = <span class="number">1</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    p1 = stoi(s1);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (s2.empty()) &#123;</span><br><span class="line">                    p2 = <span class="number">1</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    p2 = stoi(s2);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">int</span> sum = stoi(num1.substr(b1, num1.size() - b1 + <span class="number">2</span>)) + stoi(num2.substr(<span class="number">0</span>, b2 + <span class="number">1</span>));</span><br><span class="line">                <span class="keyword">int</span> eval = p1 * sum * p2;</span><br><span class="line">                <span class="keyword">if</span> (eval &lt; min_res) &#123;</span><br><span class="line">                    min_res = eval;</span><br><span class="line">                    ans = s1 + <span class="string">&quot;(&quot;</span> + num1.substr(b1, num1.size() - b1 + <span class="number">2</span>) + <span class="string">&quot;+&quot;</span> + num2.substr(<span class="number">0</span>, b2 + <span class="number">1</span>) + <span class="string">&quot;)&quot;</span> + s2;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>





<h3 id="2233-Maximum-Product-After-K-Increments"><a href="#2233-Maximum-Product-After-K-Increments" class="headerlink" title="2233. Maximum Product After K Increments"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-product-after-k-increments/">2233. Maximum Product After K Increments</a></h3><p>You are given an array of non-negative integers <code>nums</code> and an integer <code>k</code>. In one operation, you may choose <strong>any</strong> element from <code>nums</code> and <strong>increment</strong> it by <code>1</code>.</p>
<p>Return <em>the <strong>maximum</strong> <strong>product</strong> of</em> <code>nums</code> <em>after <strong>at most</strong></em> <code>k</code> <em>operations.</em> Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>. Note that you should maximize the product before taking the modulo. </p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [0,4], k &#x3D; 5</span><br><span class="line">Output: 20</span><br><span class="line">Explanation: Increment the first number 5 times.</span><br><span class="line">Now nums &#x3D; [5, 4], with a product of 5 * 4 &#x3D; 20.</span><br><span class="line">It can be shown that 20 is maximum product possible, so we return 20.</span><br><span class="line">Note that there may be other ways to increment nums to have the maximum product.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [6,3,3,2], k &#x3D; 2</span><br><span class="line">Output: 216</span><br><span class="line">Explanation: Increment the second number 1 time and increment the fourth number 1 time.</span><br><span class="line">Now nums &#x3D; [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 &#x3D; 216.</span><br><span class="line">It can be shown that 216 is maximum product possible, so we return 216.</span><br><span class="line">Note that there may be other ways to increment nums to have the maximum product.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length, k &lt;= 105</code></li>
<li><code>0 &lt;= nums[i] &lt;= 106</code></li>
</ul>
<p>Medium，找到前k大的数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumProduct</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, greater&lt;<span class="keyword">int</span>&gt;&gt; pq(nums.begin(), nums.end());</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> min_num = pq.top();</span><br><span class="line">            pq.pop();</span><br><span class="line">            pq.push(min_num + <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (!pq.empty()) &#123;</span><br><span class="line">            res = res * pq.top() % mod;</span><br><span class="line">            pq.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2234-Maximum-Total-Beauty-of-the-Gardens"><a href="#Failed-2234-Maximum-Total-Beauty-of-the-Gardens" class="headerlink" title="Failed: 2234. Maximum Total Beauty of the Gardens"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity/">2234. Maximum Total Beauty of the Gardens</a></h3><p>Alice is a caretaker of <code>n</code> gardens and she wants to plant flowers to maximize the total beauty of all her gardens.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>flowers</code> of size <code>n</code>, where <code>flowers[i]</code> is the number of flowers already planted in the <code>ith</code> garden. Flowers that are already planted <strong>cannot</strong> be removed. You are then given another integer <code>newFlowers</code>, which is the <strong>maximum</strong> number of flowers that Alice can additionally plant. You are also given the integers <code>target</code>, <code>full</code>, and <code>partial</code>.</p>
<p>A garden is considered <strong>complete</strong> if it has <strong>at least</strong> <code>target</code> flowers. The <strong>total beauty</strong> of the gardens is then determined as the <strong>sum</strong> of the following:</p>
<ul>
<li>The number of <strong>complete</strong> gardens multiplied by <code>full</code>.</li>
<li>The <strong>minimum</strong> number of flowers in any of the <strong>incomplete</strong> gardens multiplied by <code>partial</code>. If there are no incomplete gardens, then this value will be <code>0</code>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> total beauty that Alice can obtain after planting at most</em> <code>newFlowers</code> <em>flowers.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Input: flowers &#x3D; [1,3,1,1], newFlowers &#x3D; 7, target &#x3D; 6, full &#x3D; 12, partial &#x3D; 1</span><br><span class="line">Output: 14</span><br><span class="line">Explanation: Alice can plant</span><br><span class="line">- 2 flowers in the 0th garden</span><br><span class="line">- 3 flowers in the 1st garden</span><br><span class="line">- 1 flower in the 2nd garden</span><br><span class="line">- 1 flower in the 3rd garden</span><br><span class="line">The gardens will then be [3,6,2,2]. She planted a total of 2 + 3 + 1 + 1 &#x3D; 7 flowers.</span><br><span class="line">There is 1 garden that is complete.</span><br><span class="line">The minimum number of flowers in the incomplete gardens is 2.</span><br><span class="line">Thus, the total beauty is 1 * 12 + 2 * 1 &#x3D; 12 + 2 &#x3D; 14.</span><br><span class="line">No other way of planting flowers can obtain a total beauty higher than 14.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Input: flowers &#x3D; [2,4,5,3], newFlowers &#x3D; 10, target &#x3D; 5, full &#x3D; 2, partial &#x3D; 6</span><br><span class="line">Output: 30</span><br><span class="line">Explanation: Alice can plant</span><br><span class="line">- 3 flowers in the 0th garden</span><br><span class="line">- 0 flowers in the 1st garden</span><br><span class="line">- 0 flowers in the 2nd garden</span><br><span class="line">- 2 flowers in the 3rd garden</span><br><span class="line">The gardens will then be [5,4,5,5]. She planted a total of 3 + 0 + 0 + 2 &#x3D; 5 flowers.</span><br><span class="line">There are 3 gardens that are complete.</span><br><span class="line">The minimum number of flowers in the incomplete gardens is 4.</span><br><span class="line">Thus, the total beauty is 3 * 2 + 4 * 6 &#x3D; 6 + 24 &#x3D; 30.</span><br><span class="line">No other way of planting flowers can obtain a total beauty higher than 30.</span><br><span class="line">Note that Alice could make all the gardens complete but in this case, she would obtain a lower total beauty.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= flowers.length &lt;= 105</code></li>
<li><code>1 &lt;= flowers[i], target &lt;= 105</code></li>
<li><code>1 &lt;= newFlowers &lt;= 1010</code></li>
<li><code>1 &lt;= full, partial &lt;= 105</code></li>
</ul>
<p>Hard，二分查找。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">maximumBeauty</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; flowers, <span class="keyword">long</span> <span class="keyword">long</span> newFlowers, <span class="keyword">int</span> target, <span class="keyword">int</span> full, <span class="keyword">int</span> partial)</span> </span>&#123;</span><br><span class="line">        sort(flowers.begin(), flowers.end());</span><br><span class="line">        <span class="keyword">int</span> full_cnt = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = flowers.size() - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            <span class="keyword">if</span> (flowers[i] &lt; target) <span class="keyword">break</span>;</span><br><span class="line">            full_cnt++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> n = flowers.size() - full_cnt;</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">0</span>) <span class="keyword">return</span> (<span class="keyword">long</span> <span class="keyword">long</span>)full_cnt * (<span class="keyword">long</span> <span class="keyword">long</span>)full;</span><br><span class="line">        vector&lt;long long&gt; fill_up(n, 0), fill_target(n, 0);</span><br><span class="line">        <span class="comment">// fill_up: flowers needed to get min of flowers to flowers[i]</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            fill_up[i] = (flowers[i] - flowers[i - <span class="number">1</span>]) * (<span class="keyword">long</span> <span class="keyword">long</span>)i + fill_up[i - <span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// fill_target[i] fill flowers[i] to flowers[n - 1] to target level</span></span><br><span class="line">        fill_target[n - <span class="number">1</span>] = (<span class="keyword">long</span> <span class="keyword">long</span>)target - flowers[n - <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">2</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            fill_target[i] = fill_target[i + <span class="number">1</span>] + (<span class="keyword">long</span> <span class="keyword">long</span>)(target - flowers[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> num_fill = <span class="number">0</span>; num_fill &lt;= n; num_fill++) &#123;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> m = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> rm = newFlowers;</span><br><span class="line">            <span class="keyword">if</span> (num_fill != <span class="number">0</span>) &#123;</span><br><span class="line">                rm -= fill_target[n - num_fill];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (rm &lt; <span class="number">0</span>) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">if</span> (num_fill != n) &#123;</span><br><span class="line">                <span class="keyword">auto</span> ptr = upper_bound(fill_up.begin(), fill_up.end(), rm);</span><br><span class="line">                <span class="comment">// can get min to flowers[idx-1] level, but not flowers[idx] level</span></span><br><span class="line">                <span class="keyword">int</span> idx = ptr - fill_up.begin();</span><br><span class="line">                <span class="keyword">if</span>(idx &gt;= n - num_fill) idx = n - num_fill;</span><br><span class="line">                m = flowers[idx - <span class="number">1</span>];</span><br><span class="line">                m += (rm - fill_up[idx - <span class="number">1</span>]) / idx; </span><br><span class="line">                m = min(m, (<span class="keyword">long</span> <span class="keyword">long</span>)target - <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">long</span> <span class="keyword">long</span> tmp = m * (<span class="keyword">long</span> <span class="keyword">long</span>) partial + (full_cnt + num_fill) * (<span class="keyword">long</span> <span class="keyword">long</span>) full;</span><br><span class="line">            res = max(tmp, res);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-289"><a href="#Weekly-Contest-289" class="headerlink" title="Weekly Contest 289"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-289/">Weekly Contest 289</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515163243.png" alt="image-20220515163243533"></p>
<h3 id="2243-Calculate-Digit-Sum-of-a-String"><a href="#2243-Calculate-Digit-Sum-of-a-String" class="headerlink" title="2243. Calculate Digit Sum of a String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/calculate-digit-sum-of-a-string/">2243. Calculate Digit Sum of a String</a></h3><p>You are given a string <code>s</code> consisting of digits and an integer <code>k</code>.</p>
<p>A <strong>round</strong> can be completed if the length of <code>s</code> is greater than <code>k</code>. In one round, do the following:</p>
<ol>
<li><strong>Divide</strong> <code>s</code> into <strong>consecutive groups</strong> of size <code>k</code> such that the first <code>k</code> characters are in the first group, the next <code>k</code> characters are in the second group, and so on. <strong>Note</strong> that the size of the last group can be smaller than <code>k</code>.</li>
<li><strong>Replace</strong> each group of <code>s</code> with a string representing the sum of all its digits. For example, <code>&quot;346&quot;</code> is replaced with <code>&quot;13&quot;</code> because <code>3 + 4 + 6 = 13</code>.</li>
<li><strong>Merge</strong> consecutive groups together to form a new string. If the length of the string is greater than <code>k</code>, repeat from step <code>1</code>.</li>
</ol>
<p>Return <code>s</code> <em>after all rounds have been completed</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;11111222223&quot;, k &#x3D; 3</span><br><span class="line">Output: &quot;135&quot;</span><br><span class="line">Explanation: </span><br><span class="line">- For the first round, we divide s into groups of size 3: &quot;111&quot;, &quot;112&quot;, &quot;222&quot;, and &quot;23&quot;.</span><br><span class="line">  Then we calculate the digit sum of each group: 1 + 1 + 1 &#x3D; 3, 1 + 1 + 2 &#x3D; 4, 2 + 2 + 2 &#x3D; 6, and 2 + 3 &#x3D; 5. </span><br><span class="line">  So, s becomes &quot;3&quot; + &quot;4&quot; + &quot;6&quot; + &quot;5&quot; &#x3D; &quot;3465&quot; after the first round.</span><br><span class="line">- For the second round, we divide s into &quot;346&quot; and &quot;5&quot;.</span><br><span class="line">  Then we calculate the digit sum of each group: 3 + 4 + 6 &#x3D; 13, 5 &#x3D; 5. </span><br><span class="line">  So, s becomes &quot;13&quot; + &quot;5&quot; &#x3D; &quot;135&quot; after second round. </span><br><span class="line">Now, s.length &lt;&#x3D; k, so we return &quot;135&quot; as the answer.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;00000000&quot;, k &#x3D; 3</span><br><span class="line">Output: &quot;000&quot;</span><br><span class="line">Explanation: </span><br><span class="line">We divide s into &quot;000&quot;, &quot;000&quot;, and &quot;00&quot;.</span><br><span class="line">Then we calculate the digit sum of each group: 0 + 0 + 0 &#x3D; 0, 0 + 0 + 0 &#x3D; 0, and 0 + 0 &#x3D; 0. </span><br><span class="line">s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; &#x3D; &quot;000&quot;, whose length is equal to k, so we return &quot;000&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>2 &lt;= k &lt;= 100</code></li>
<li><code>s</code> consists of digits only.</li>
</ul>
<p>Easy，暴力遍历每轮的个数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">digitSum</span><span class="params">(<span class="built_in">string</span> s, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (s.length() &gt; k) &#123;</span><br><span class="line">            <span class="built_in">string</span> temp = <span class="string">&quot;&quot;</span>;</span><br><span class="line">            <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.length(); i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i != <span class="number">0</span> &amp;&amp; i % k == <span class="number">0</span>) &#123;</span><br><span class="line">                    temp += to_string(sum);</span><br><span class="line">                    sum = s[i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    sum += (s[i] - <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            temp += to_string(sum);</span><br><span class="line">            s = temp;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> s;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2244-Minimum-Rounds-to-Complete-All-Tasks"><a href="#2244-Minimum-Rounds-to-Complete-All-Tasks" class="headerlink" title="2244. Minimum Rounds to Complete All Tasks"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/">2244. Minimum Rounds to Complete All Tasks</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to complete all the tasks, or</em> <code>-1</code> <em>if it is not possible to complete all the tasks.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: tasks &#x3D; [2,2,3,3,2,4,4,4,4,4]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: To complete all the tasks, a possible plan is:</span><br><span class="line">- In the first round, you complete 3 tasks of difficulty level 2. </span><br><span class="line">- In the second round, you complete 2 tasks of difficulty level 3. </span><br><span class="line">- In the third round, you complete 3 tasks of difficulty level 4. </span><br><span class="line">- In the fourth round, you complete 2 tasks of difficulty level 4.  </span><br><span class="line">It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: tasks &#x3D; [2,3,3]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><p><code>1 &lt;= tasks.length &lt;= 105</code></p>
</li>
<li><p><code>1 &lt;= tasks[i] &lt;= 109</code></p>
<p>Medium，计算每个数的出现的次数，根据次数计算需要消耗的次数。</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumRounds</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; tasks)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; count;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> task: tasks) &#123;</span><br><span class="line">            <span class="keyword">if</span> (count.find(task) == count.end()) &#123;</span><br><span class="line">                count[task] = <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                count[task]++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> it = count.begin(); it != count.end(); it++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (it-&gt;second == <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            sum += countRound(it-&gt;second);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> sum;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countRound</span><span class="params">(<span class="keyword">int</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num % <span class="number">3</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> num / <span class="number">3</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> num / <span class="number">3</span> + <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2245-Maximum-Trailing-Zeros-in-a-Cornered-Path"><a href="#2245-Maximum-Trailing-Zeros-in-a-Cornered-Path" class="headerlink" title="2245. Maximum Trailing Zeros in a Cornered Path"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-trailing-zeros-in-a-cornered-path/">2245. Maximum Trailing Zeros in a Cornered Path</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>tasks</code>, where <code>tasks[i]</code> represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the <strong>same difficulty level</strong>.</p>
<p>Return <em>the <strong>minimum</strong> rounds required to complete all the tasks, or</em> <code>-1</code> <em>if it is not possible to complete all the tasks.</em></p>
<p>You are given a 2D integer array <code>grid</code> of size <code>m x n</code>, where each cell contains a positive integer.</p>
<p>A <strong>cornered path</strong> is defined as a set of adjacent cells with <strong>at most</strong> one turn. More specifically, the path should exclusively move either <strong>horizontally</strong> or <strong>vertically</strong> up to the turn (if there is one), without returning to a previously visited cell. After the turn, the path will then move exclusively in the <strong>alternate</strong> direction: move vertically if it moved horizontally, and vice versa, also without returning to a previously visited cell.</p>
<p>The <strong>product</strong> of a path is defined as the product of all the values in the path.</p>
<p>Return <em>the <strong>maximum</strong> number of <strong>trailing zeros</strong> in the product of a cornered path found in</em> <code>grid</code>.</p>
<p>Note:</p>
<ul>
<li><strong>Horizontal</strong> movement means moving in either the left or right direction.</li>
<li><strong>Vertical</strong> movement means moving in either the up or down direction.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203205.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[23,17,15,3,20],[8,1,20,27,11],[9,4,6,2,21],[40,9,1,10,6],[22,7,4,5,3]]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The grid on the left shows a valid cornered path.</span><br><span class="line">It has a product of 15 * 20 * 6 * 1 * 10 &#x3D; 18000 which has 3 trailing zeros.</span><br><span class="line">It can be shown that this is the maximum trailing zeros in the product of a cornered path.</span><br><span class="line"></span><br><span class="line">The grid in the middle is not a cornered path as it has more than one turn.</span><br><span class="line">The grid on the right is not a cornered path as it requires a return to a previously visited cell.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203156.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[4,3,2],[7,6,1],[8,8,8]]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: The grid is shown in the figure above.</span><br><span class="line">There are no cornered paths in the grid that result in a product with a trailing zero.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><p><code>m == grid.length</code></p>
</li>
<li><p><code>n == grid[i].length</code></p>
</li>
<li><p><code>1 &lt;= m, n &lt;= 105</code></p>
</li>
<li><p><code>1 &lt;= m * n &lt;= 105</code></p>
</li>
<li><p><code>1 &lt;= grid[i][j] &lt;= 1000</code></p>
<p>Medium，计算每个数的上下左右直线能到达的长度，计算每个角的长度。</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">int</span> m, n;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maxTrailingZeros</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        m = grid.size(), n = grid[<span class="number">0</span>].size();</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;&gt; count(m, <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;(n));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                count[i][j] = countTwoAndFive(grid[i][j]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;&gt; left(m, <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;(n));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            left[i][<span class="number">0</span>].first = count[i][<span class="number">0</span>].first, left[i][<span class="number">0</span>].second = count[i][<span class="number">0</span>].second;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; n; j++) &#123;</span><br><span class="line">                left[i][j].first = left[i][j - <span class="number">1</span>].first + count[i][j].first, left[i][j].second = left[i][j - <span class="number">1</span>].second + count[i][j].second;</span><br><span class="line">                <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;&quot; &quot;&lt;&lt;left[i][j].first&lt;&lt;&quot; &quot;&lt;&lt;left[i][j].second&lt;&lt;endl;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;&gt; right(m, <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;(n));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            right[i][n - <span class="number">1</span>].first = count[i][n - <span class="number">1</span>].first, right[i][n - <span class="number">1</span>].second = count[i][n - <span class="number">1</span>].second;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = n - <span class="number">2</span>; j &gt;= <span class="number">0</span>; j--) &#123;</span><br><span class="line">                right[i][j].first =  right[i][j + <span class="number">1</span>].first + count[i][j].first, right[i][j].second = right[i][j + <span class="number">1</span>].second + count[i][j].second;</span><br><span class="line">                <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;&quot; &quot;&lt;&lt;right[i][j].first&lt;&lt;&quot; &quot;&lt;&lt;right[i][j].second&lt;&lt;endl;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt; up(n, <span class="built_in">make_pair</span>(<span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (min(up[j].first + left[i][j].first, up[j].second + left[i][j].second) &gt; res) &#123;</span><br><span class="line">                    res = min(up[j].first + left[i][j].first, up[j].second + left[i][j].second);</span><br><span class="line">                    <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;&quot; &quot;&lt;&lt;up[j].first + left[i][j].first&lt;&lt;&quot; &quot;&lt;&lt;up[j].second + left[i][j].second&lt;&lt;endl;</span></span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (min(up[j].first + right[i][j].first, up[j].second + right[i][j].second) &gt; res) &#123;</span><br><span class="line">                    res = min(up[j].first + right[i][j].first, up[j].second + right[i][j].second);</span><br><span class="line">                    <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;&quot; &quot;&lt;&lt;up[j].first + right[i][j].first&lt;&lt;&quot; &quot;&lt;&lt;up[j].second + right[i][j].second&lt;&lt;endl;</span></span><br><span class="line">                &#125;</span><br><span class="line">                up[j].first += count[i][j].first, up[j].second += count[i][j].second;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">pair</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt; bottom(n, <span class="built_in">make_pair</span>(<span class="number">0</span>, <span class="number">0</span>));</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = m - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (min(bottom[j].first + left[i][j].first, bottom[j].second + left[i][j].second) &gt; res) &#123;</span><br><span class="line">                    res = min(bottom[j].first + left[i][j].first, bottom[j].second + left[i][j].second);</span><br><span class="line">                    <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;endl;</span></span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (min(bottom[j].first + right[i][j].first, bottom[j].second + right[i][j].second) &gt; res) &#123;</span><br><span class="line">                    res = min(bottom[j].first + right[i][j].first, bottom[j].second + right[i][j].second);</span><br><span class="line">                    <span class="comment">// cout&lt;&lt;i&lt;&lt;&quot; &quot;&lt;&lt;j&lt;&lt;endl;</span></span><br><span class="line">                &#125;</span><br><span class="line">                bottom[j].first += count[i][j].first, bottom[j].second += count[i][j].second;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    pair&lt;int, int&gt; countTwoAndFive(int num) &#123;</span><br><span class="line">        <span class="keyword">int</span> countTwo = <span class="number">0</span>, countFive = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (num % <span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            num /= <span class="number">2</span>;</span><br><span class="line">            countTwo++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (num % <span class="number">5</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            num /= <span class="number">5</span>;</span><br><span class="line">            countFive++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">make_pair</span>(countTwo, countFive);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2246-Longest-Path-With-Different-Adjacent-Characters"><a href="#Failed-2246-Longest-Path-With-Different-Adjacent-Characters" class="headerlink" title="Failed: 2246. Longest Path With Different Adjacent Characters"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-path-with-different-adjacent-characters/">2246. Longest Path With Different Adjacent Characters</a></h3><p>You are given a <strong>tree</strong> (i.e. a connected, undirected graph that has no cycles) <strong>rooted</strong> at node <code>0</code> consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. The tree is represented by a <strong>0-indexed</strong> array <code>parent</code> of size <code>n</code>, where <code>parent[i]</code> is the parent of node <code>i</code>. Since node <code>0</code> is the root, <code>parent[0] == -1</code>.</p>
<p>You are also given a string <code>s</code> of length <code>n</code>, where <code>s[i]</code> is the character assigned to node <code>i</code>.</p>
<p>Return <em>the length of the <strong>longest path</strong> in the tree such that no pair of <strong>adjacent</strong> nodes on the path have the same character assigned to them.</em></p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515203532.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: parent &#x3D; [-1,0,0,1,1,2], s &#x3D; &quot;abacbe&quot;</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The longest path where each two adjacent nodes have different characters in the tree is the path: 0 -&gt; 1 -&gt; 3. The length of this path is 3, so 3 is returned.</span><br><span class="line">It can be proven that there is no longer path that satisfies the conditions. </span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515212734.png" alt="graph2drawio"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: parent &#x3D; [-1,0,0,0], s &#x3D; &quot;aabc&quot;</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The longest path where each two adjacent nodes have different characters is the path: 2 -&gt; 0 -&gt; 3. The length of this path is 3, so 3 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == parent.length == s.length</code></li>
<li><code>1 &lt;= n &lt;= 105</code></li>
<li><code>0 &lt;= parent[i] &lt;= n - 1</code> for all <code>i &gt;= 1</code></li>
<li><code>parent[0] == -1</code></li>
<li><code>parent</code> represents a valid tree.</li>
<li><code>s</code> consists of only lowercase English letters.</li>
</ul>
<p>Hard，找到父节点，进行BFS或DFS。</p>
<p>DFS</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestPath</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; parent, <span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = s.size(), res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; children(n, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;());</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            children[parent[i]].push_back(i);</span><br><span class="line">        &#125;</span><br><span class="line">        dfs(children, s, res, <span class="number">0</span>);</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">dfs</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; children, <span class="built_in">string</span>&amp; s, <span class="keyword">int</span>&amp; res, <span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> big1 = <span class="number">0</span>, big2 = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> &amp;j: children[i]) &#123;</span><br><span class="line">            <span class="keyword">int</span> cur = dfs(children, s, res, j);</span><br><span class="line">            <span class="keyword">if</span> (s[i] == s[j]) <span class="keyword">continue</span>;</span><br><span class="line">            <span class="keyword">if</span> (cur &gt; big2) big2 = cur;</span><br><span class="line">            <span class="keyword">if</span> (big2 &gt; big1) swap(big1, big2);</span><br><span class="line">        &#125;</span><br><span class="line">        res = max(res, big1 + big2 + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> big1 + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>BFS</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestPath</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; parent, <span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = parent.size(), res = <span class="number">1</span>;</span><br><span class="line">        vector&lt;int&gt; cnt(n), top1(n, 1), top2(n, 1);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++)</span><br><span class="line">            cnt[parent[i]]++;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; q;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++)</span><br><span class="line">            <span class="keyword">if</span> (cnt[i] == <span class="number">0</span>)</span><br><span class="line">                q.push_back(i);</span><br><span class="line">        <span class="keyword">while</span> (!q.empty() &amp;&amp; q.back() != <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; q1;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i : q) &#123;</span><br><span class="line">                <span class="keyword">int</span> p = parent[i];</span><br><span class="line">                <span class="keyword">int</span> length = <span class="number">1</span> + (s[i] != s[p] ? top1[i] : <span class="number">0</span>);</span><br><span class="line">                <span class="keyword">if</span> (top1[p] &lt;= length) &#123;</span><br><span class="line">                    top2[p] = top1[p];</span><br><span class="line">                    top1[p] = length;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span></span><br><span class="line">                    top2[p] = max(top2[p], length);</span><br><span class="line">                <span class="keyword">if</span> (--cnt[p] == <span class="number">0</span>) &#123;</span><br><span class="line">                    q1.push_back(p);</span><br><span class="line">                    res = max(res, top1[p] + top2[p] - <span class="number">1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            swap(q, q1);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-291"><a href="#Weekly-Contest-291" class="headerlink" title="Weekly Contest 291"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-291/">Weekly Contest 291</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160454.png" alt="Weekly Contest 291"></p>
<h3 id="2259-Remove-Digit-From-Number-to-Maximize-Result"><a href="#2259-Remove-Digit-From-Number-to-Maximize-Result" class="headerlink" title="2259. Remove Digit From Number to Maximize Result"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/remove-digit-from-number-to-maximize-result/">2259. Remove Digit From Number to Maximize Result</a></h3><p>You are given a string <code>number</code> representing a <strong>positive integer</strong> and a character <code>digit</code>.</p>
<p>Return <em>the resulting string after removing <strong>exactly one occurrence</strong> of</em> <code>digit</code> <em>from</em> <code>number</code> <em>such that the value of the resulting string in <strong>decimal</strong> form is <strong>maximized</strong></em>. The test cases are generated such that <code>digit</code> occurs at least once in <code>number</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: number &#x3D; &quot;123&quot;, digit &#x3D; &quot;3&quot;</span><br><span class="line">Output: &quot;12&quot;</span><br><span class="line">Explanation: There is only one &#39;3&#39; in &quot;123&quot;. After removing &#39;3&#39;, the result is &quot;12&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: number &#x3D; &quot;1231&quot;, digit &#x3D; &quot;1&quot;</span><br><span class="line">Output: &quot;231&quot;</span><br><span class="line">Explanation: We can remove the first &#39;1&#39; to get &quot;231&quot; or remove the second &#39;1&#39; to get &quot;123&quot;.</span><br><span class="line">Since 231 &gt; 123, we return &quot;231&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: number &#x3D; &quot;551&quot;, digit &#x3D; &quot;5&quot;</span><br><span class="line">Output: &quot;51&quot;</span><br><span class="line">Explanation: We can remove either the first or second &#39;5&#39; from &quot;551&quot;.</span><br><span class="line">Both result in the string &quot;51&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= number.length &lt;= 100</code></li>
<li><code>number</code> consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li>
<li><code>digit</code> is a digit from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li>
<li><code>digit</code> occurs at least once in <code>number</code>.</li>
</ul>
<p>Easy，就是暴力，自己string用得不熟，写的太烂</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">removeDigit</span><span class="params">(<span class="built_in">string</span> number, <span class="keyword">char</span> digit)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">int</span> last;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = number.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; ) &#123;</span><br><span class="line">            <span class="keyword">if</span> (number[i] == digit) &#123;</span><br><span class="line">                <span class="built_in">string</span> left = i &gt; <span class="number">0</span> ? number.substr(<span class="number">0</span>, i) : <span class="string">&quot;&quot;</span>;</span><br><span class="line">                <span class="comment">// cout&lt;&lt;left&lt;&lt;endl;</span></span><br><span class="line">                <span class="built_in">string</span> right = i &lt; n - <span class="number">1</span> ? number.substr(i + <span class="number">1</span>, n - i - <span class="number">1</span>) : <span class="string">&quot;&quot;</span>;</span><br><span class="line">                <span class="comment">// cout&lt;&lt;right&lt;&lt;endl;</span></span><br><span class="line">                <span class="built_in">string</span> temp = left + right;</span><br><span class="line">                <span class="keyword">if</span> (res == <span class="string">&quot;&quot;</span>) &#123;</span><br><span class="line">                    res = temp;</span><br><span class="line">                    last = i;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> j = last; j &lt; i; j++) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (temp[j] &gt; res[j]) &#123;</span><br><span class="line">                            res = temp;</span><br><span class="line">                            last = i;</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (temp[j] &lt; res[j]) &#123;</span><br><span class="line">                            <span class="keyword">break</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">while</span> (i &lt; n &amp;&amp; number[i] == digit) i++;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>别人写得比较好的</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">removeDigit</span><span class="params">(<span class="built_in">string</span> number, <span class="keyword">char</span> digit)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> ans = <span class="string">&quot;0&quot;</span>;</span><br><span class="line">        <span class="keyword">int</span> n = number.length();</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; number.length(); i++)&#123;</span><br><span class="line">            <span class="keyword">if</span>(number[i] == digit)&#123;</span><br><span class="line">                <span class="built_in">string</span> temp = number.substr(<span class="number">0</span>, i) + number.substr(i + <span class="number">1</span>, n - i);</span><br><span class="line">                <span class="keyword">if</span>(temp &gt; ans)&#123;</span><br><span class="line">                    ans = temp;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2260-Minimum-Consecutive-Cards-to-Pick-Up"><a href="#2260-Minimum-Consecutive-Cards-to-Pick-Up" class="headerlink" title="2260. Minimum Consecutive Cards to Pick Up"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-consecutive-cards-to-pick-up/">2260. Minimum Consecutive Cards to Pick Up</a></h3><p>You are given an integer array <code>cards</code> where <code>cards[i]</code> represents the <strong>value</strong> of the <code>ith</code> card. A pair of cards are <strong>matching</strong> if the cards have the <strong>same</strong> value.</p>
<p>Return <em>the <strong>minimum</strong> number of <strong>consecutive</strong> cards you have to pick up to have a pair of <strong>matching</strong> cards among the picked cards.</em> If it is impossible to have matching cards, return <code>-1</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: cards &#x3D; [3,4,2,3,4,7]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: We can pick up the cards [3,4,2,3] which contain a matching pair of cards with value 3. Note that picking up the cards [4,2,3,4] is also optimal.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: cards &#x3D; [1,0,5,3]</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: There is no way to pick up a set of consecutive cards that contain a pair of matching cards.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= cards.length &lt;= 105</code></li>
<li><code>0 &lt;= cards[i] &lt;= 106</code></li>
</ul>
<p>Medium，保存每个最后出现的字母个数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumCardPickup</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; cards)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; last;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = cards.size();</span><br><span class="line">        <span class="keyword">int</span> res = n + <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (last.find(cards[i]) != last.end()) &#123;</span><br><span class="line">                res = min(res, i - last[cards[i]] + <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            last[cards[i]] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (res == n + <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> res;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2261-K-Divisible-Elements-Subarrays"><a href="#2261-K-Divisible-Elements-Subarrays" class="headerlink" title="2261. K Divisible Elements Subarrays"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/k-divisible-elements-subarrays/">2261. K Divisible Elements Subarrays</a></h3><p>Given an integer array <code>nums</code> and two integers <code>k</code> and <code>p</code>, return <em>the number of <strong>distinct subarrays</strong> which have <strong>at most</strong></em> <code>k</code> <em>elements divisible by</em> <code>p</code>.</p>
<p>Two arrays <code>nums1</code> and <code>nums2</code> are said to be <strong>distinct</strong> if:</p>
<ul>
<li>They are of <strong>different</strong> lengths, or</li>
<li>There exists <strong>at least</strong> one index <code>i</code> where <code>nums1[i] != nums2[i]</code>.</li>
</ul>
<p>A <strong>subarray</strong> is defined as a <strong>non-empty</strong> contiguous sequence of elements in an array.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,3,3,2,2], k &#x3D; 2, p &#x3D; 2</span><br><span class="line">Output: 11</span><br><span class="line">Explanation:</span><br><span class="line">The elements at indices 0, 3, and 4 are divisible by p &#x3D; 2.</span><br><span class="line">The 11 distinct subarrays which have at most k &#x3D; 2 elements divisible by 2 are:</span><br><span class="line">[2], [2,3], [2,3,3], [2,3,3,2], [3], [3,3], [3,3,2], [3,3,2,2], [3,2], [3,2,2], and [2,2].</span><br><span class="line">Note that the subarrays [2] and [3] occur more than once in nums, but they should each be counted only once.</span><br><span class="line">The subarray [2,3,3,2,2] should not be counted because it has 3 elements that are divisible by 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,3,4], k &#x3D; 4, p &#x3D; 1</span><br><span class="line">Output: 10</span><br><span class="line">Explanation:</span><br><span class="line">All element of nums are divisible by p &#x3D; 1.</span><br><span class="line">Also, every subarray of nums will have at most 4 elements that are divisible by 1.</span><br><span class="line">Since all subarrays are distinct, the total number of subarrays satisfying all the constraints is 10.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>1 &lt;= nums[i], p &lt;= 200</code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>
<p>Mediu，以字符串形式保存每个符合要求的子数组</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countDistinct</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k, <span class="keyword">int</span> p)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_set</span>&lt;<span class="built_in">string</span>&gt; visited;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> left = <span class="number">0</span>; left &lt; nums.size(); left++) &#123;</span><br><span class="line">            <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">            <span class="built_in">string</span> temp = <span class="string">&quot;&quot;</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> right = left; right &lt; nums.size(); right++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (nums[right] % p == <span class="number">0</span>) &#123;</span><br><span class="line">                    count++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (count &gt; k) <span class="keyword">break</span>;</span><br><span class="line">                temp += to_string(nums[right]) + <span class="string">&quot;#&quot;</span>;</span><br><span class="line">                visited.insert(temp);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> visited.size();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2262-Total-Appeal-of-A-String"><a href="#Failed-2262-Total-Appeal-of-A-String" class="headerlink" title="Failed: 2262. Total Appeal of A String"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/total-appeal-of-a-string/">2262. Total Appeal of A String</a></h3><p>The <strong>appeal</strong> of a string is the number of <strong>distinct</strong> characters found in the string.</p>
<ul>
<li>For example, the appeal of <code>&quot;abbca&quot;</code> is <code>3</code> because it has <code>3</code> distinct characters: <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li>
</ul>
<p>Given a string <code>s</code>, return <em>the *<em>total appeal of all of its \</em></em>substrings*<em>.**</em></p>
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abbca&quot;</span><br><span class="line">Output: 28</span><br><span class="line">Explanation: The following are the substrings of &quot;abbca&quot;:</span><br><span class="line">- Substrings of length 1: &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot; have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.</span><br><span class="line">- Substrings of length 2: &quot;ab&quot;, &quot;bb&quot;, &quot;bc&quot;, &quot;ca&quot; have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.</span><br><span class="line">- Substrings of length 3: &quot;abb&quot;, &quot;bbc&quot;, &quot;bca&quot; have an appeal of 2, 2, and 3 respectively. The sum is 7.</span><br><span class="line">- Substrings of length 4: &quot;abbc&quot;, &quot;bbca&quot; have an appeal of 3 and 3 respectively. The sum is 6.</span><br><span class="line">- Substrings of length 5: &quot;abbca&quot; has an appeal of 3. The sum is 3.</span><br><span class="line">The total sum is 5 + 7 + 7 + 6 + 3 &#x3D; 28.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;code&quot;</span><br><span class="line">Output: 20</span><br><span class="line">Explanation: The following are the substrings of &quot;code&quot;:</span><br><span class="line">- Substrings of length 1: &quot;c&quot;, &quot;o&quot;, &quot;d&quot;, &quot;e&quot; have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.</span><br><span class="line">- Substrings of length 2: &quot;co&quot;, &quot;od&quot;, &quot;de&quot; have an appeal of 2, 2, and 2 respectively. The sum is 6.</span><br><span class="line">- Substrings of length 3: &quot;cod&quot;, &quot;ode&quot; have an appeal of 3 and 3 respectively. The sum is 6.</span><br><span class="line">- Substrings of length 4: &quot;code&quot; has an appeal of 4. The sum is 4.</span><br><span class="line">The total sum is 4 + 6 + 6 + 4 &#x3D; 20.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 105</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
<p>Hard，其实可以很简单，保存每个字母当前位置，以及以当前位置结尾的子数组的个数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">appealSum</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>, cur = <span class="number">0</span>, prev[<span class="number">26</span>] = &#123;&#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; s.size(); i++) &#123;</span><br><span class="line">            cur += i + <span class="number">1</span> - prev[s[i] - <span class="string">&#x27;a&#x27;</span>];</span><br><span class="line">            prev[s[i] - <span class="string">&#x27;a&#x27;</span>] = i + <span class="number">1</span>;</span><br><span class="line">            res += cur;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-292"><a href="#Weekly-Contest-292" class="headerlink" title="Weekly Contest 292"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-292/">Weekly Contest 292</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515151456.png" alt="Weekly Contest 292"></p>
<h3 id="2264-Largest-3-Same-Digit-Number-in-String"><a href="#2264-Largest-3-Same-Digit-Number-in-String" class="headerlink" title="2264. Largest 3-Same-Digit Number in String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/">2264. Largest 3-Same-Digit Number in String</a></h3><p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p>
<ul>
<li>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</li>
<li>It consists of only one unique digit.</li>
</ul>
<p>Return <em>the <strong>maximum good</strong> integer as a <strong>string</strong> or an empty string</em> <code>&quot;&quot;</code> <em>if no such integer exists</em>.</p>
<p>Note:</p>
<ul>
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
<li>There may be <strong>leading zeroes</strong> in <code>num</code> or a good integer.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; &quot;6777133339&quot;</span><br><span class="line">Output: &quot;777&quot;</span><br><span class="line">Explanation: There are two distinct good integers: &quot;777&quot; and &quot;333&quot;.</span><br><span class="line">&quot;777&quot; is the largest, so we return &quot;777&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; &quot;2300019&quot;</span><br><span class="line">Output: &quot;000&quot;</span><br><span class="line">Explanation: &quot;000&quot; is the only good integer.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; &quot;42352338&quot;</span><br><span class="line">Output: &quot;&quot;</span><br><span class="line">Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= num.length &lt;= 1000</code></li>
<li><code>num</code> only consists of digits.</li>
</ul>
<p>Easy，按个遍历每个位置上的重复数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">largestGoodInteger</span><span class="params">(<span class="built_in">string</span> num)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">-1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; num.size() - <span class="number">2</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (num[i + <span class="number">1</span>] == num[i] &amp;&amp; num[i + <span class="number">2</span>] == num[i]) &#123;</span><br><span class="line">                <span class="keyword">if</span> (num[i] - <span class="string">&#x27;0&#x27;</span> &gt; ans) &#123;</span><br><span class="line">                    ans = num[i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                i += <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (ans == <span class="number">-1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> to_string(ans) + to_string(ans) + to_string(ans);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2265-Count-Nodes-Equal-to-Average-of-Subtree"><a href="#2265-Count-Nodes-Equal-to-Average-of-Subtree" class="headerlink" title="2265. Count Nodes Equal to Average of Subtree"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/">2265. Count Nodes Equal to Average of Subtree</a></h3><p>You are given a string <code>num</code> representing a large integer. An integer is <strong>good</strong> if it meets the following conditions:</p>
<ul>
<li><p>It is a <strong>substring</strong> of <code>num</code> with length <code>3</code>.</p>
</li>
<li><p>It consists of only one unique digit.</p>
</li>
</ul>
<p>Given the <code>root</code> of a binary tree, return <em>the number of nodes where the value of the node is equal to the <strong>average</strong> of the values in its <strong>subtree</strong></em>.</p>
<p><strong>Note:</strong></p>
<ul>
<li>The <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</li>
<li>A <strong>subtree</strong> of <code>root</code> is a tree consisting of <code>root</code> and all of its descendants.</li>
</ul>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515154208.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [4,8,5,0,1,null,6]</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: </span><br><span class="line">For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) &#x2F; 6 &#x3D; 24 &#x2F; 6 &#x3D; 4.</span><br><span class="line">For the node with value 5: The average of its subtree is (5 + 6) &#x2F; 2 &#x3D; 11 &#x2F; 2 &#x3D; 5.</span><br><span class="line">For the node with value 0: The average of its subtree is 0 &#x2F; 1 &#x3D; 0.</span><br><span class="line">For the node with value 1: The average of its subtree is 1 &#x2F; 1 &#x3D; 1.</span><br><span class="line">For the node with value 6: The average of its subtree is 6 &#x2F; 1 &#x3D; 6.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515154215.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: root &#x3D; [1]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: For the node with value 1: The average of its subtree is 1 &#x2F; 1 &#x3D; 1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
</ul>
<p>Medium，dfs后序遍历</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * struct TreeNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     TreeNode *left;</span></span><br><span class="line"><span class="comment"> *     TreeNode *right;</span></span><br><span class="line"><span class="comment"> *     TreeNode() : val(0), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">averageOfSubtree</span><span class="params">(TreeNode* root)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        dfs(root, ans);</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">    pair&lt;int, int&gt; dfs(TreeNode* root, int &amp;ans) &#123;</span><br><span class="line">        <span class="keyword">if</span> (root == <span class="literal">nullptr</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">make_pair</span>(<span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> sum = root-&gt;val, count = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (root-&gt;left != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">            <span class="keyword">auto</span> left_ans = dfs(root-&gt;left, ans);</span><br><span class="line">            sum += left_ans.first;</span><br><span class="line">            count += left_ans.second;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (root-&gt;right != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">            <span class="keyword">auto</span> right_ans = dfs(root-&gt;right, ans);</span><br><span class="line">            sum += right_ans.first;</span><br><span class="line">            count += right_ans.second;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (sum / count == root-&gt;val) &#123;</span><br><span class="line">            ans++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">make_pair</span>(sum, count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2266-Count-Number-of-Texts"><a href="#2266-Count-Number-of-Texts" class="headerlink" title="2266. Count Number of Texts"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-number-of-texts/">2266. Count Number of Texts</a></h3><p>Alice is texting Bob using her phone. The <strong>mapping</strong> of digits to letters is shown in the figure below.</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160205.png" alt="img"></p>
<p>In order to <strong>add</strong> a letter, Alice has to <strong>press</strong> the key of the corresponding digit <code>i</code> times, where <code>i</code> is the position of the letter in the key.</p>
<ul>
<li>For example, to add the letter <code>&#39;s&#39;</code>, Alice has to press <code>&#39;7&#39;</code> four times. Similarly, to add the letter <code>&#39;k&#39;</code>, Alice has to press <code>&#39;5&#39;</code> twice.</li>
<li>Note that the digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code> do not map to any letters, so Alice <strong>does not</strong> use them.</li>
</ul>
<p>However, due to an error in transmission, Bob did not receive Alice’s text message but received a <strong>string of pressed keys</strong> instead.</p>
<ul>
<li>For example, when Alice sent the message <code>&quot;bob&quot;</code>, Bob received the string <code>&quot;2266622&quot;</code>.</li>
</ul>
<p>Given a string <code>pressedKeys</code> representing the string received by Bob, return <em>the <strong>total number of possible text messages</strong> Alice could have sent</em>.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: pressedKeys &#x3D; &quot;22233&quot;</span><br><span class="line">Output: 8</span><br><span class="line">Explanation:</span><br><span class="line">The possible text messages Alice could have sent are:</span><br><span class="line">&quot;aaadd&quot;, &quot;abdd&quot;, &quot;badd&quot;, &quot;cdd&quot;, &quot;aaae&quot;, &quot;abe&quot;, &quot;bae&quot;, and &quot;ce&quot;.</span><br><span class="line">Since there are 8 possible messages, we return 8.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: pressedKeys &#x3D; &quot;222222222222222222222222222222222222&quot;</span><br><span class="line">Output: 82876089</span><br><span class="line">Explanation:</span><br><span class="line">There are 2082876103 possible text messages Alice could have sent.</span><br><span class="line">Since we need to return the answer modulo 109 + 7, we return 2082876103 % (109 + 7) &#x3D; 82876089.</span><br></pre></td></tr></table></figure>



<p><strong>Constraints:</strong></p>
<ul>
<li><p><code>1 &lt;= pressedKeys.length &lt;= 105</code></p>
</li>
<li><p><code>pressedKeys</code> only consists of digits from <code>&#39;2&#39;</code> - <code>&#39;9&#39;</code>.</p>
<p>Medium，动态规划，注意第二重循环长度取决于当前字母在之前的重复次数。一开始DFS严重超时。</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="keyword">const</span> <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; pad = &#123;</span><br><span class="line">        <span class="string">&quot;&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;abc&quot;</span>, <span class="string">&quot;def&quot;</span>,</span><br><span class="line">        <span class="string">&quot;ghi&quot;</span>, <span class="string">&quot;jkl&quot;</span>, <span class="string">&quot;mno&quot;</span>,</span><br><span class="line">        <span class="string">&quot;pqrs&quot;</span>, <span class="string">&quot;tuv&quot;</span>, <span class="string">&quot;wxyz&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countTexts</span><span class="params">(<span class="built_in">string</span> pressedKeys)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = pressedKeys.length();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">ans</span><span class="params">(n + <span class="number">1</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        ans[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="built_in">string</span> word = pad[pressedKeys[i] - <span class="string">&#x27;0&#x27;</span>];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i; j &gt;= <span class="number">0</span> &amp;&amp; i - j &lt; word.length() &amp;&amp; pressedKeys[i] == pressedKeys[j]; j--) &#123;</span><br><span class="line">                ans[i + <span class="number">1</span>] += ans[j];</span><br><span class="line">                ans[i + <span class="number">1</span>] %= <span class="number">1000000007</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> ans[n] % <span class="number">1000000007</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2267-Check-if-There-Is-a-Valid-Parentheses-String-Path"><a href="#Failed-2267-Check-if-There-Is-a-Valid-Parentheses-String-Path" class="headerlink" title="Failed: 2267. Check if There Is a Valid Parentheses String Path"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/check-if-there-is-a-valid-parentheses-string-path/">2267. Check if There Is a Valid Parentheses String Path</a></h3><p>A parentheses string is a <strong>non-empty</strong> string consisting only of <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>. It is <strong>valid</strong> if <strong>any</strong> of the following conditions is <strong>true</strong>:</p>
<ul>
<li>It is <code>()</code>.</li>
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid parentheses strings.</li>
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li>
</ul>
<p>You are given an <code>m x n</code> matrix of parentheses <code>grid</code>. A <strong>valid parentheses string path</strong> in the grid is a path satisfying <strong>all</strong> of the following conditions:</p>
<ul>
<li>The path starts from the upper left cell <code>(0, 0)</code>.</li>
<li>The path ends at the bottom-right cell <code>(m - 1, n - 1)</code>.</li>
<li>The path only ever moves <strong>down</strong> or <strong>right</strong>.</li>
<li>The resulting parentheses string formed by the path is <strong>valid</strong>.</li>
</ul>
<p>Return <code>true</code> <em>if there exists a <strong>valid parentheses string path</strong> in the grid.</em> Otherwise, return <code>false</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515155724.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[&quot;(&quot;,&quot;(&quot;,&quot;(&quot;],[&quot;)&quot;,&quot;(&quot;,&quot;)&quot;],[&quot;(&quot;,&quot;(&quot;,&quot;)&quot;],[&quot;(&quot;,&quot;(&quot;,&quot;)&quot;]]</span><br><span class="line">Output: true</span><br><span class="line">Explanation: The above diagram shows two possible paths that form valid parentheses strings.</span><br><span class="line">The first path shown results in the valid parentheses string &quot;()(())&quot;.</span><br><span class="line">The second path shown results in the valid parentheses string &quot;((()))&quot;.</span><br><span class="line">Note that there may be other valid parentheses string paths.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515160233.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[&quot;)&quot;,&quot;)&quot;],[&quot;(&quot;,&quot;(&quot;]]</span><br><span class="line">Output: false</span><br><span class="line">Explanation: The two possible paths form the parentheses strings &quot;))(&quot; and &quot;)((&quot;. Since neither of them are valid parentheses strings, we return false.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 100</code></li>
<li><code>grid[i][j]</code> is either <code>&#39;(&#39;</code> or <code>&#39;)&#39;</code>.</li>
</ul>
<p>Hard，三重DP，我的命门，又是DFS超时</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">hasValidPath</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">char</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m = grid.size(), n = grid[<span class="number">0</span>].size(), maxk = (m + n + <span class="number">1</span>) / <span class="number">2</span>;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&gt; dp(m + <span class="number">1</span>, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;(n + <span class="number">1</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(maxk + <span class="number">10</span>)));</span><br><span class="line">        dp[<span class="number">0</span>][<span class="number">0</span>][<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++)</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++)</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">1</span>; k &lt;= maxk; k++) &#123;</span><br><span class="line">                    dp[i][j + <span class="number">1</span>][k] |= dp[i][j][k + (grid[i][j] == <span class="string">&#x27;(&#x27;</span> ? <span class="number">-1</span> : <span class="number">1</span>)];</span><br><span class="line">                    dp[i + <span class="number">1</span>][j][k] |= dp[i][j][k + (grid[i][j] == <span class="string">&#x27;(&#x27;</span> ? <span class="number">-1</span> : <span class="number">1</span>)];</span><br><span class="line">                &#125;</span><br><span class="line">        <span class="keyword">return</span> dp[m][n - <span class="number">1</span>][<span class="number">1</span>];       </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-293"><a href="#Weekly-Contest-293" class="headerlink" title="Weekly Contest 293"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-293/">Weekly Contest 293</a></h2><p>在25岁生日前夕第一次收获了AK，十分激动。</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220515121603.png" alt="Weekly Contest 293"></p>
<h3 id="2273-Find-Resultant-Array-After-Removing-Anagrams"><a href="#2273-Find-Resultant-Array-After-Removing-Anagrams" class="headerlink" title="2273. Find Resultant Array After Removing Anagrams"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/">2273. Find Resultant Array After Removing Anagrams</a></h3><p>You are given a <strong>0-indexed</strong> string array <code>words</code>, where <code>words[i]</code> consists of lowercase English letters.</p>
<p>In one operation, select any index <code>i</code> such that <code>0 &lt; i &lt; words.length</code> and <code>words[i - 1]</code> and <code>words[i]</code> are <strong>anagrams</strong>, and <strong>delete</strong> <code>words[i]</code> from <code>words</code>. Keep performing this operation as long as you can select an index that satisfies the conditions.</p>
<p>Return <code>words</code> <em>after performing all operations</em>. It can be shown that selecting the indices for each operation in <strong>any</strong> arbitrary order will lead to the same result.</p>
<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example, <code>&quot;dacb&quot;</code> is an anagram of <code>&quot;abdc&quot;</code>.</p>
<p> <strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Input: words &#x3D; [&quot;abba&quot;,&quot;baba&quot;,&quot;bbaa&quot;,&quot;cd&quot;,&quot;cd&quot;]</span><br><span class="line">Output: [&quot;abba&quot;,&quot;cd&quot;]</span><br><span class="line">Explanation:</span><br><span class="line">One of the ways we can obtain the resultant array is by using the following operations:</span><br><span class="line">- Since words[2] &#x3D; &quot;bbaa&quot; and words[1] &#x3D; &quot;baba&quot; are anagrams, we choose index 2 and delete words[2].</span><br><span class="line">  Now words &#x3D; [&quot;abba&quot;,&quot;baba&quot;,&quot;cd&quot;,&quot;cd&quot;].</span><br><span class="line">- Since words[1] &#x3D; &quot;baba&quot; and words[0] &#x3D; &quot;abba&quot; are anagrams, we choose index 1 and delete words[1].</span><br><span class="line">  Now words &#x3D; [&quot;abba&quot;,&quot;cd&quot;,&quot;cd&quot;].</span><br><span class="line">- Since words[2] &#x3D; &quot;cd&quot; and words[1] &#x3D; &quot;cd&quot; are anagrams, we choose index 2 and delete words[2].</span><br><span class="line">  Now words &#x3D; [&quot;abba&quot;,&quot;cd&quot;].</span><br><span class="line">We can no longer perform any operations, so [&quot;abba&quot;,&quot;cd&quot;] is the final answer.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: words &#x3D; [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;]</span><br><span class="line">Output: [&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;]</span><br><span class="line">Explanation:</span><br><span class="line">No two adjacent strings in words are anagrams of each other, so no operations are performed.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>
<p>easy，单词排序记录各字母及其出现次数。一开始没搞懂题意做错了。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; <span class="title">removeAnagrams</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; words)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> prev;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt; res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">string</span> word: words) &#123;</span><br><span class="line">            <span class="built_in">string</span> wordSort = word;</span><br><span class="line">            sort(wordSort.begin(), wordSort.end());</span><br><span class="line">            <span class="keyword">if</span> (wordSort != prev) &#123;</span><br><span class="line">                prev = wordSort;</span><br><span class="line">                res.push_back(word);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2274-Maximum-Consecutive-Floors-Without-Special-Floors"><a href="#2274-Maximum-Consecutive-Floors-Without-Special-Floors" class="headerlink" title="2274. Maximum Consecutive Floors Without Special Floors"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/">2274. Maximum Consecutive Floors Without Special Floors</a></h3><p>Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be <strong>special floors</strong>, used for relaxation only.</p>
<p>You are given two integers <code>bottom</code> and <code>top</code>, which denote that Alice has rented all the floors from <code>bottom</code> to <code>top</code> (<strong>inclusive</strong>). You are also given the integer array <code>special</code>, where <code>special[i]</code> denotes a special floor that Alice has designated for relaxation.</p>
<p>Return <em>the <strong>maximum</strong> number of consecutive floors without a special floor</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: bottom &#x3D; 2, top &#x3D; 9, special &#x3D; [4,6]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The following are the ranges (inclusive) of consecutive floors without a special floor:</span><br><span class="line">- (2, 3) with a total amount of 2 floors.</span><br><span class="line">- (5, 5) with a total amount of 1 floor.</span><br><span class="line">- (7, 9) with a total amount of 3 floors.</span><br><span class="line">Therefore, we return the maximum number which is 3 floors.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: bottom &#x3D; 6, top &#x3D; 8, special &#x3D; [7,6,8]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: Every floor rented is a special floor, so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= special.length &lt;= 105</code></li>
<li><code>1 &lt;= bottom &lt;= special[i] &lt;= top &lt;= 109</code></li>
<li>All the values of <code>special</code> are <strong>unique</strong>.</li>
</ul>
<p>Medium，排序记录上一个楼层，注意一下边界。同样因为没搞清题意错了一次</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">class Solution &#123;</span><br><span class="line">public:</span><br><span class="line">    int maxConsecutive(int bottom, int top, vector&lt;int&gt;&amp; special) &#123;</span><br><span class="line">        sort(special.begin(), special.end());</span><br><span class="line">        int res &#x3D; 0;</span><br><span class="line">        int prev &#x3D; bottom - 1;</span><br><span class="line">        for (int sp: special) &#123;</span><br><span class="line">            if (sp - prev - 1 &gt; res) &#123;</span><br><span class="line">                res &#x3D; sp - prev - 1;</span><br><span class="line">            &#125;</span><br><span class="line">            prev &#x3D; sp;</span><br><span class="line">        &#125;</span><br><span class="line">        if (top - prev &gt; res) res &#x3D; top - prev;</span><br><span class="line">        return res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero"><a href="#2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero" class="headerlink" title="2275. Largest Combination With Bitwise AND Greater Than Zero"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/">2275. Largest Combination With Bitwise AND Greater Than Zero</a></h3><p>Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be <strong>special floors</strong>, used for relaxation only.</p>
<p>You are given two integers <code>bottom</code> and <code>top</code>, which denote that Alice has rented all the floors from <code>bottom</code> to <code>top</code> (<strong>inclusive</strong>). You are also given the integer array <code>special</code>, where <code>special[i]</code> denotes a special floor that Alice has designated for relaxation.</p>
<p>Return <em>the <strong>maximum</strong> number of consecutive floors without a special floor</em>.</p>
<p>The <strong>bitwise AND</strong> of an array <code>nums</code> is the bitwise AND of all integers in <code>nums</code>.</p>
<ul>
<li>For example, for <code>nums = [1, 5, 3]</code>, the bitwise AND is equal to <code>1 &amp; 5 &amp; 3 = 1</code>.</li>
<li>Also, for <code>nums = [7]</code>, the bitwise AND is <code>7</code>.</li>
</ul>
<p>You are given an array of positive integers <code>candidates</code>. Evaluate the <strong>bitwise AND</strong> of every <strong>combination</strong> of numbers of <code>candidates</code>. Each number in <code>candidates</code> may only be used <strong>once</strong> in each combination.</p>
<p>Return <em>the size of the <strong>largest</strong> combination of</em> <code>candidates</code> <em>with a bitwise AND <strong>greater</strong> than</em> <code>0</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: candidates &#x3D; [16,17,71,62,12,24,14]</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: The combination [16,17,62,24] has a bitwise AND of 16 &amp; 17 &amp; 62 &amp; 24 &#x3D; 16 &gt; 0.</span><br><span class="line">The size of the combination is 4.</span><br><span class="line">It can be shown that no combination with a size greater than 4 has a bitwise AND greater than 0.</span><br><span class="line">Note that more than one combination may have the largest size.</span><br><span class="line">For example, the combination [62,12,24,14] has a bitwise AND of 62 &amp; 12 &amp; 24 &amp; 14 &#x3D; 8 &gt; 0.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: candidates &#x3D; [8,8]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: The largest combination [8,8] has a bitwise AND of 8 &amp; 8 &#x3D; 8 &gt; 0.</span><br><span class="line">The size of the combination is 2, so we return 2.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= candidates.length &lt;= 105</code></li>
<li><code>1 &lt;= candidates[i] &lt;= 107</code></li>
</ul>
<p>Medium，遍历每一位，找到该位有最多的数。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">largestCombination</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; candidates)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">count</span> <span class="params">(<span class="number">25</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> candidate: candidates) &#123;</span><br><span class="line">            <span class="keyword">int</span> i = <span class="number">0</span>, cur = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">while</span> (candidate != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (candidate &amp; cur) &#123;</span><br><span class="line">                    count[i]++;</span><br><span class="line">                    candidate ^= cur;</span><br><span class="line">                &#125;</span><br><span class="line">                i++;</span><br><span class="line">                cur &lt;&lt;= <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">25</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (count[i] &gt; res) res = count[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2276-Count-Integers-in-Intervals"><a href="#2276-Count-Integers-in-Intervals" class="headerlink" title="2276. Count Integers in Intervals"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-integers-in-intervals/">2276. Count Integers in Intervals</a></h3><p>Given an <strong>empty</strong> set of intervals, implement a data structure that can:</p>
<ul>
<li><strong>Add</strong> an interval to the set of intervals.</li>
<li><strong>Count</strong> the number of integers that are present in <strong>at least one</strong> interval.</li>
</ul>
<p>Implement the <code>CountIntervals</code> class:</p>
<ul>
<li><code>CountIntervals()</code> Initializes the object with an empty set of intervals.</li>
<li><code>void add(int left, int right)</code> Adds the interval <code>[left, right]</code> to the set of intervals.</li>
<li><code>int count()</code> Returns the number of integers that are present in <strong>at least one</strong> interval.</li>
</ul>
<p><strong>Note</strong> that an interval <code>[left, right]</code> denotes all the integers <code>x</code> where <code>left &lt;= x &lt;= right</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">Input</span><br><span class="line">[&quot;CountIntervals&quot;, &quot;add&quot;, &quot;add&quot;, &quot;count&quot;, &quot;add&quot;, &quot;count&quot;]</span><br><span class="line">[[], [2, 3], [7, 10], [], [5, 8], []]</span><br><span class="line">Output</span><br><span class="line">[null, null, null, 6, null, 8]</span><br><span class="line"></span><br><span class="line">Explanation</span><br><span class="line">CountIntervals countIntervals &#x3D; new CountIntervals(); &#x2F;&#x2F; initialize the object with an empty set of intervals. </span><br><span class="line">countIntervals.add(2, 3);  &#x2F;&#x2F; add [2, 3] to the set of intervals.</span><br><span class="line">countIntervals.add(7, 10); &#x2F;&#x2F; add [7, 10] to the set of intervals.</span><br><span class="line">countIntervals.count();    &#x2F;&#x2F; return 6</span><br><span class="line">                           &#x2F;&#x2F; the integers 2 and 3 are present in the interval [2, 3].</span><br><span class="line">                           &#x2F;&#x2F; the integers 7, 8, 9, and 10 are present in the interval [7, 10].</span><br><span class="line">countIntervals.add(5, 8);  &#x2F;&#x2F; add [5, 8] to the set of intervals.</span><br><span class="line">countIntervals.count();    &#x2F;&#x2F; return 8</span><br><span class="line">                           &#x2F;&#x2F; the integers 2 and 3 are present in the interval [2, 3].</span><br><span class="line">                           &#x2F;&#x2F; the integers 5 and 6 are present in the interval [5, 8].</span><br><span class="line">                           &#x2F;&#x2F; the integers 7 and 8 are present in the intervals [5, 8] and [7, 10].</span><br><span class="line">                           &#x2F;&#x2F; the integers 9 and 10 are present in the interval [7, 10].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= left &lt;= right &lt;= 109</code></li>
<li>At most <code>105</code> calls <strong>in total</strong> will be made to <code>add</code> and <code>count</code>.</li>
<li>At least <strong>one</strong> call will be made to <code>count</code>.</li>
</ul>
<p>Hard，使用map保存每一个interval的开始和结尾，插入时注意原先interval的删除。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CountIntervals</span> &#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="built_in">map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; intervals;</span><br><span class="line">    <span class="keyword">int</span> len = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    CountIntervals() &#123;</span><br><span class="line">    </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> left, <span class="keyword">int</span> right)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">auto</span> iter = intervals.upper_bound(right);</span><br><span class="line">        <span class="keyword">if</span> (iter == intervals.begin()) &#123;</span><br><span class="line">            intervals[left] = right;</span><br><span class="line">            len += right - left + <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">return</span> ;</span><br><span class="line">        &#125;</span><br><span class="line">        iter--;</span><br><span class="line">        <span class="keyword">while</span>(<span class="literal">true</span>) &#123;</span><br><span class="line">            <span class="keyword">if</span> (iter-&gt;second &lt; left) <span class="keyword">break</span>;</span><br><span class="line">            len -= (iter-&gt;second - iter-&gt;first + <span class="number">1</span>);</span><br><span class="line">            left = min(left, iter-&gt;first);</span><br><span class="line">            right = max(right, iter-&gt;second);</span><br><span class="line">            iter = intervals.erase(iter);</span><br><span class="line">            <span class="keyword">if</span> (iter == intervals.begin()) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">else</span> iter--;</span><br><span class="line">        &#125;</span><br><span class="line">        intervals[left] = right;</span><br><span class="line">        len += right - left + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">count</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> len;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your CountIntervals object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * CountIntervals* obj = new CountIntervals();</span></span><br><span class="line"><span class="comment"> * obj-&gt;add(left,right);</span></span><br><span class="line"><span class="comment"> * int param_2 = obj-&gt;count();</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-294"><a href="#Weekly-Contest-294" class="headerlink" title="Weekly Contest 294"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-294/">Weekly Contest 294</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522144035.png" alt="image-20220522144029784"></p>
<h3 id="2278-Percentage-of-Letter-in-String"><a href="#2278-Percentage-of-Letter-in-String" class="headerlink" title="2278. Percentage of Letter in String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/percentage-of-letter-in-string/">2278. Percentage of Letter in String</a></h3><p>Given a string <code>s</code> and a character <code>letter</code>, return <em>the <strong>percentage</strong> of characters in</em> <code>s</code> <em>that equal</em> <code>letter</code> *<strong>rounded down</strong> to the nearest whole percent.*</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;foobar&quot;, letter &#x3D; &quot;o&quot;</span><br><span class="line">Output: 33</span><br><span class="line">Explanation:</span><br><span class="line">The percentage of characters in s that equal the letter &#39;o&#39; is 2 &#x2F; 6 * 100% &#x3D; 33% when rounded down, so we return 33.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;jjjj&quot;, letter &#x3D; &quot;k&quot;</span><br><span class="line">Output: 0</span><br><span class="line">Explanation:</span><br><span class="line">The percentage of characters in s that equal the letter &#39;k&#39; is 0%, so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>letter</code> is a lowercase English letter.</li>
</ul>
<p>Easy，很简单的计数</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">percentageLetter</span><span class="params">(<span class="built_in">string</span> s, <span class="keyword">char</span> letter)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">const</span> <span class="keyword">int</span> n = s.size();</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (s[i] == letter) &#123;</span><br><span class="line">                count++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> count * <span class="number">100</span> / n;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2279-Maximum-Bags-With-Full-Capacity-of-Rocks"><a href="#2279-Maximum-Bags-With-Full-Capacity-of-Rocks" class="headerlink" title="2279. Maximum Bags With Full Capacity of Rocks"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/">2279. Maximum Bags With Full Capacity of Rocks</a></h3><p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>ith</code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</code> rocks. You are also given an integer <code>additionalRocks</code>, the number of additional rocks you can place in <strong>any</strong> of the bags.</p>
<p>Return <em>the <strong>maximum</strong> number of bags that could have full capacity after placing the additional rocks in some bags.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: capacity &#x3D; [2,3,4,5], rocks &#x3D; [1,2,4,4], additionalRocks &#x3D; 2</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">Place 1 rock in bag 0 and 1 rock in bag 1.</span><br><span class="line">The number of rocks in each bag are now [2,3,4,4].</span><br><span class="line">Bags 0, 1, and 2 have full capacity.</span><br><span class="line">There are 3 bags at full capacity, so we return 3.</span><br><span class="line">It can be shown that it is not possible to have more than 3 bags at full capacity.</span><br><span class="line">Note that there may be other ways of placing the rocks that result in an answer of 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: capacity &#x3D; [10,2,2], rocks &#x3D; [2,2,0], additionalRocks &#x3D; 100</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">Place 8 rocks in bag 0 and 2 rocks in bag 2.</span><br><span class="line">The number of rocks in each bag are now [10,2,2].</span><br><span class="line">Bags 0, 1, and 2 have full capacity.</span><br><span class="line">There are 3 bags at full capacity, so we return 3.</span><br><span class="line">It can be shown that it is not possible to have more than 3 bags at full capacity.</span><br><span class="line">Note that we did not use all of the additional rocks.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == capacity.length == rocks.length</code></li>
<li><code>1 &lt;= n &lt;= 5 * 104</code></li>
<li><code>1 &lt;= capacity[i] &lt;= 109</code></li>
<li><code>0 &lt;= rocks[i] &lt;= capacity[i]</code></li>
<li><code>1 &lt;= additionalRocks &lt;= 109</code></li>
</ul>
<p>Medium，做差，采用的最小堆选取最小的差</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumBags</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; capacity, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; rocks, <span class="keyword">int</span> additionalRocks)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;<span class="keyword">int</span>, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;, greater&lt;<span class="keyword">int</span>&gt;&gt; pq;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; capacity.size(); i++) &#123;</span><br><span class="line">            pq.push(capacity[i] - rocks[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">while</span> (!pq.empty()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (pq.top() &gt; additionalRocks) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                res++;</span><br><span class="line">                additionalRocks -= pq.top();</span><br><span class="line">                pq.pop();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2280-Minimum-Lines-to-Represent-a-Line-Chart"><a href="#2280-Minimum-Lines-to-Represent-a-Line-Chart" class="headerlink" title="2280. Minimum Lines to Represent a Line Chart"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/">2280. Minimum Lines to Represent a Line Chart</a></h3><p>You are given a 2D integer array <code>stockPrices</code> where <code>stockPrices[i] = [dayi, pricei]</code> indicates the price of the stock on day <code>dayi</code> is <code>pricei</code>. A <strong>line chart</strong> is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points. One such example is shown below:</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522150904.png" alt="img"></p>
<p>Return <em>the <strong>minimum number of lines</strong> needed to represent the line chart</em>.</p>
<p> <strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522151350.png" alt="ex0"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: stockPrices &#x3D; [[1,7],[2,6],[3,5],[4,4],[5,4],[6,3],[7,2],[8,1]]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">The diagram above represents the input, with the X-axis representing the day and Y-axis representing the price.</span><br><span class="line">The following 3 lines can be drawn to represent the line chart:</span><br><span class="line">- Line 1 (in red) from (1,7) to (4,4) passing through (1,7), (2,6), (3,5), and (4,4).</span><br><span class="line">- Line 2 (in blue) from (4,4) to (5,4).</span><br><span class="line">- Line 3 (in green) from (5,4) to (8,1) passing through (5,4), (6,3), (7,2), and (8,1).</span><br><span class="line">It can be shown that it is not possible to represent the line chart using less than 3 lines.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220522151412.png" alt="ex1"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: stockPrices &#x3D; [[3,4],[1,2],[7,8],[2,3]]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">As shown in the diagram above, the line chart can be represented with a single line.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= stockPrices.length &lt;= 105</code></li>
<li><code>stockPrices[i].length == 2</code></li>
<li><code>1 &lt;= dayi, pricei &lt;= 109</code></li>
<li>All <code>dayi</code> are <strong>distinct</strong>.</li>
</ul>
<p>Medium，判断与之前的2点是否共线，务必注意用除法计算有精度误差。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumLines</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; stockPrices)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (stockPrices.size() == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        sort(stockPrices.begin(), stockPrices.end(), </span><br><span class="line">            [](<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp; a, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp; b) -&gt; <span class="keyword">bool</span> &#123; </span><br><span class="line">                <span class="keyword">return</span> a[<span class="number">0</span>] &lt; b[<span class="number">0</span>]; </span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> pre = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; stockPrices.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (i &gt; pre + <span class="number">1</span> &amp;&amp; !isSameLine(stockPrices[pre][<span class="number">0</span>], stockPrices[pre][<span class="number">1</span>], stockPrices[i - <span class="number">1</span>][<span class="number">0</span>], stockPrices[i - <span class="number">1</span>][<span class="number">1</span>], stockPrices[i][<span class="number">0</span>], stockPrices[i][<span class="number">1</span>])) &#123;</span><br><span class="line">                res++;</span><br><span class="line">                pre = i - <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">isSameLine</span><span class="params">(<span class="keyword">int</span> x0, <span class="keyword">int</span> y0, <span class="keyword">int</span> x1, <span class="keyword">int</span> y1, <span class="keyword">int</span> x2, <span class="keyword">int</span> y2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">long</span> <span class="keyword">long</span>)(y1 - y0) * (<span class="keyword">long</span> <span class="keyword">long</span>)(x2 - x0) == (<span class="keyword">long</span> <span class="keyword">long</span>)(y2 - y0) * (<span class="keyword">long</span> <span class="keyword">long</span>)(x1 - x0);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2281-Sum-of-Total-Strength-of-Wizards"><a href="#Failed-2281-Sum-of-Total-Strength-of-Wizards" class="headerlink" title="Failed: 2281. Sum of Total Strength of Wizards"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/sum-of-total-strength-of-wizards/">2281. Sum of Total Strength of Wizards</a></h3><p>As the ruler of a kingdom, you have an army of wizards at your command.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>ith</code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the wizards’ strengths form a <strong>subarray</strong> of <code>strength</code>), the <strong>total strength</strong> is defined as the <strong>product</strong> of the following two values:</p>
<ul>
<li>The strength of the <strong>weakest</strong> wizard in the group.</li>
<li>The <strong>total</strong> of all the individual strengths of the wizards in the group.</li>
</ul>
<p>Return <em>the <strong>sum</strong> of the total strengths of <strong>all</strong> contiguous groups of wizards</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Input: strength &#x3D; [1,3,1,2]</span><br><span class="line">Output: 44</span><br><span class="line">Explanation: The following are all the contiguous groups of wizards:</span><br><span class="line">- [1] from [1,3,1,2] has a total strength of min([1]) * sum([1]) &#x3D; 1 * 1 &#x3D; 1</span><br><span class="line">- [3] from [1,3,1,2] has a total strength of min([3]) * sum([3]) &#x3D; 3 * 3 &#x3D; 9</span><br><span class="line">- [1] from [1,3,1,2] has a total strength of min([1]) * sum([1]) &#x3D; 1 * 1 &#x3D; 1</span><br><span class="line">- [2] from [1,3,1,2] has a total strength of min([2]) * sum([2]) &#x3D; 2 * 2 &#x3D; 4</span><br><span class="line">- [1,3] from [1,3,1,2] has a total strength of min([1,3]) * sum([1,3]) &#x3D; 1 * 4 &#x3D; 4</span><br><span class="line">- [3,1] from [1,3,1,2] has a total strength of min([3,1]) * sum([3,1]) &#x3D; 1 * 4 &#x3D; 4</span><br><span class="line">- [1,2] from [1,3,1,2] has a total strength of min([1,2]) * sum([1,2]) &#x3D; 1 * 3 &#x3D; 3</span><br><span class="line">- [1,3,1] from [1,3,1,2] has a total strength of min([1,3,1]) * sum([1,3,1]) &#x3D; 1 * 5 &#x3D; 5</span><br><span class="line">- [3,1,2] from [1,3,1,2] has a total strength of min([3,1,2]) * sum([3,1,2]) &#x3D; 1 * 6 &#x3D; 6</span><br><span class="line">- [1,3,1,2] from [1,3,1,2] has a total strength of min([1,3,1,2]) * sum([1,3,1,2]) &#x3D; 1 * 7 &#x3D; 7</span><br><span class="line">The sum of all the total strengths is 1 + 9 + 1 + 4 + 4 + 4 + 3 + 5 + 6 + 7 &#x3D; 44.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Input: strength &#x3D; [5,4,6]</span><br><span class="line">Output: 213</span><br><span class="line">Explanation: The following are all the contiguous groups of wizards: </span><br><span class="line">- [5] from [5,4,6] has a total strength of min([5]) * sum([5]) &#x3D; 5 * 5 &#x3D; 25</span><br><span class="line">- [4] from [5,4,6] has a total strength of min([4]) * sum([4]) &#x3D; 4 * 4 &#x3D; 16</span><br><span class="line">- [6] from [5,4,6] has a total strength of min([6]) * sum([6]) &#x3D; 6 * 6 &#x3D; 36</span><br><span class="line">- [5,4] from [5,4,6] has a total strength of min([5,4]) * sum([5,4]) &#x3D; 4 * 9 &#x3D; 36</span><br><span class="line">- [4,6] from [5,4,6] has a total strength of min([4,6]) * sum([4,6]) &#x3D; 4 * 10 &#x3D; 40</span><br><span class="line">- [5,4,6] from [5,4,6] has a total strength of min([5,4,6]) * sum([5,4,6]) &#x3D; 4 * 15 &#x3D; 60</span><br><span class="line">The sum of all the total strengths is 25 + 16 + 36 + 36 + 40 + 60 &#x3D; 213.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= strength.length &lt;= 105</code></li>
<li><code>1 &lt;= strength[i] &lt;= 109</code></li>
</ul>
<p>Hard, 学到了新的东西，prefixMul，单纯用prefixSum还是会超时</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">totalStrength</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; strength)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> n = strength.size(), mod = <span class="number">1000000007</span>;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line">        vector&lt;long long&gt; preSumLeft(n + 1), preSumRight(n + 1);</span><br><span class="line">        vector&lt;long long&gt; preMulLeft(n + 1), preMulRight(n + 1);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            preSumLeft[i + <span class="number">1</span>] = (preSumLeft[i] + strength[i]) % mod;</span><br><span class="line">            preMulLeft[i + <span class="number">1</span>] = (preMulLeft[i] + strength[i] * (<span class="keyword">long</span> <span class="keyword">long</span>)(i + <span class="number">1</span>)) % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            preSumRight[i] = (preSumRight[i + <span class="number">1</span>] + strength[i]) % mod;</span><br><span class="line">            preMulRight[i] = (preMulRight[i + <span class="number">1</span>] + strength[i] * (<span class="keyword">long</span> <span class="keyword">long</span>)(n - i)) % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; stk;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= n; i++) &#123;</span><br><span class="line">            <span class="keyword">while</span> (!stk.empty() &amp;&amp; (i == n || strength[stk.top()] &gt;= strength[i])) &#123;</span><br><span class="line">                <span class="keyword">int</span> pivot = stk.top();</span><br><span class="line">                stk.pop();</span><br><span class="line">                <span class="keyword">int</span> pre = stk.empty() ? <span class="number">0</span> : stk.top() + <span class="number">1</span>;</span><br><span class="line">                <span class="keyword">long</span> <span class="keyword">long</span> left_sum = (preMulLeft[pivot + <span class="number">1</span>] - preMulLeft[pre] - pre * (preSumLeft[pivot + <span class="number">1</span>] - preSumLeft[pre])) % mod;</span><br><span class="line">                <span class="keyword">long</span> <span class="keyword">long</span> right_sum = (preMulRight[pivot + <span class="number">1</span>] - preMulRight[i] - (n - i) * (preSumRight[pivot + <span class="number">1</span>] - preSumRight[i])) % mod;</span><br><span class="line">                <span class="keyword">long</span> <span class="keyword">long</span> all_sum = (right_sum * (pivot - pre + <span class="number">1</span>) + left_sum * (i - pivot)) % mod;</span><br><span class="line">                res = (res + all_sum * strength[pivot]) % mod;</span><br><span class="line">            &#125;</span><br><span class="line">            stk.push(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-295"><a href="#Weekly-Contest-295" class="headerlink" title="Weekly Contest 295"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-295/">Weekly Contest 295</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529142047.png" alt="image-20220529142039839"></p>
<h3 id="2287-Rearrange-Characters-to-Make-Target-String"><a href="#2287-Rearrange-Characters-to-Make-Target-String" class="headerlink" title="2287. Rearrange Characters to Make Target String"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/">2287. Rearrange Characters to Make Target String</a></h3><p>You are given two <strong>0-indexed</strong> strings <code>s</code> and <code>target</code>. You can take some letters from <code>s</code> and rearrange them to form new strings.</p>
<p>Return <em>the <strong>maximum</strong> number of copies of</em> <code>target</code> <em>that can be formed by taking letters from</em> <code>s</code> <em>and rearranging them.</em></p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;ilovecodingonleetcode&quot;, target &#x3D; &quot;code&quot;</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">For the first copy of &quot;code&quot;, take the letters at indices 4, 5, 6, and 7.</span><br><span class="line">For the second copy of &quot;code&quot;, take the letters at indices 17, 18, 19, and 20.</span><br><span class="line">The strings that are formed are &quot;ecod&quot; and &quot;code&quot; which can both be rearranged into &quot;code&quot;.</span><br><span class="line">We can make at most two copies of &quot;code&quot;, so we return 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abcba&quot;, target &#x3D; &quot;abc&quot;</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">We can make one copy of &quot;abc&quot; by taking the letters at indices 0, 1, and 2.</span><br><span class="line">We can make at most one copy of &quot;abc&quot;, so we return 1.</span><br><span class="line">Note that while there is an extra &#39;a&#39; and &#39;b&#39; at indices 3 and 4, we cannot reuse the letter &#39;c&#39; at index 2, so we cannot make a second copy of &quot;abc&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;abbaccaddaeea&quot;, target &#x3D; &quot;aaaaa&quot;</span><br><span class="line">Output: 1</span><br><span class="line">Explanation:</span><br><span class="line">We can make one copy of &quot;aaaaa&quot; by taking the letters at indices 0, 3, 6, 9, and 12.</span><br><span class="line">We can make at most one copy of &quot;aaaaa&quot;, so we return 1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>1 &lt;= target.length &lt;= 10</code></li>
<li><code>s</code> and <code>target</code> consist of lowercase English letters.</li>
</ul>
<p>Easy，统计各字符数量</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">rearrangeCharacters</span><span class="params">(<span class="built_in">string</span> s, <span class="built_in">string</span> target)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">index</span><span class="params">(<span class="number">26</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: s) &#123;</span><br><span class="line">            index[c - <span class="string">&#x27;a&#x27;</span>]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">t</span><span class="params">(<span class="number">26</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: target) &#123;</span><br><span class="line">            t[c - <span class="string">&#x27;a&#x27;</span>]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = s.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: target) &#123;</span><br><span class="line">            <span class="keyword">int</span> diff = c - <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">            res = min(res, index[diff] / t[diff]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2288-Apply-Discount-to-Prices"><a href="#2288-Apply-Discount-to-Prices" class="headerlink" title="2288. Apply Discount to Prices"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/apply-discount-to-prices/">2288. Apply Discount to Prices</a></h3><p>A <strong>sentence</strong> is a string of single-space separated words where each word can contain digits, lowercase letters, and the dollar sign <code>&#39;$&#39;</code>. A word represents a <strong>price</strong> if it is a non-negative real number preceded by a dollar sign.</p>
<ul>
<li>For example, <code>&quot;$100&quot;</code>, <code>&quot;$23&quot;</code>, and <code>&quot;$6.75&quot;</code> represent prices while <code>&quot;100&quot;</code>, <code>&quot;$&quot;</code>, and <code>&quot;2$3&quot;</code> do not.</li>
</ul>
<p>You are given a string <code>sentence</code> representing a sentence and an integer <code>discount</code>. For each word representing a price, apply a discount of <code>discount%</code> on the price and <strong>update</strong> the word in the sentence. All updated prices should be represented with <strong>exactly two</strong> decimal places.</p>
<p>Return <em>a string representing the modified sentence</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: sentence &#x3D; &quot;there are $1 $2 and 5$ candies in the shop&quot;, discount &#x3D; 50</span><br><span class="line">Output: &quot;there are $0.50 $1.00 and 5$ candies in the shop&quot;</span><br><span class="line">Explanation: </span><br><span class="line">The words which represent prices are &quot;$1&quot; and &quot;$2&quot;. </span><br><span class="line">- A 50% discount on &quot;$1&quot; yields &quot;$0.50&quot;, so &quot;$1&quot; is replaced by &quot;$0.50&quot;.</span><br><span class="line">- A 50% discount on &quot;$2&quot; yields &quot;$1&quot;. Since we need to have exactly 2 decimal places after a price, we replace &quot;$2&quot; with &quot;$1.00&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: sentence &#x3D; &quot;1 2 $3 4 $5 $6 7 8$ $9 $10$&quot;, discount &#x3D; 100</span><br><span class="line">Output: &quot;1 2 $0.00 4 $0.00 $0.00 7 8$ $0.00 $10$&quot;</span><br><span class="line">Explanation: </span><br><span class="line">Applying a 100% discount on any price will result in 0.</span><br><span class="line">The words representing prices are &quot;$3&quot;, &quot;$5&quot;, &quot;$6&quot;, and &quot;$9&quot;.</span><br><span class="line">Each of them is replaced by &quot;$0.00&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= sentence.length &lt;= 105</code></li>
<li><code>sentence</code> consists of lowercase English letters, digits, <code>&#39; &#39;</code>, and <code>&#39;$&#39;</code>.</li>
<li><code>sentence</code> does not have leading or trailing spaces.</li>
<li>All words in <code>sentence</code> are separated by a single space.</li>
<li>All prices will be <strong>positive</strong> integers without leading zeros.</li>
<li>All prices will have <strong>at most</strong> <code>10</code> digits.</li>
<li><code>0 &lt;= discount &lt;= 100</code></li>
</ul>
<p>Medium，这题坑在题目没说明白，其实只有前面是$后面是空格才需要转换，以及对计算精度扣的太死</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">discountPrices</span><span class="params">(<span class="built_in">string</span> sentence, <span class="keyword">int</span> discount)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sentence.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">char</span> c = sentence[i];</span><br><span class="line">            <span class="keyword">if</span> (c == <span class="string">&#x27;$&#x27;</span> &amp;&amp; (i == <span class="number">0</span> || sentence[i - <span class="number">1</span>] == <span class="string">&#x27; &#x27;</span>)) &#123;</span><br><span class="line">                res += c;</span><br><span class="line">                i++;</span><br><span class="line">                <span class="built_in">string</span> cur;</span><br><span class="line">                <span class="keyword">while</span> (i &lt; sentence.size() &amp;&amp; sentence[i] &gt;= <span class="string">&#x27;0&#x27;</span> &amp;&amp; sentence[i] &lt;= <span class="string">&#x27;9&#x27;</span>) &#123;</span><br><span class="line">                    cur += sentence[i];</span><br><span class="line">                    i++;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (cur != <span class="string">&quot;&quot;</span> &amp;&amp; ((i &lt; sentence.size() &amp;&amp; sentence[i] == <span class="string">&#x27; &#x27;</span>) || i == sentence.size())) &#123;</span><br><span class="line">                    <span class="keyword">long</span> <span class="keyword">double</span> num = stod(cur);</span><br><span class="line">                    <span class="built_in">string</span> str = to_string(num / <span class="number">100</span> * (<span class="number">100</span> - discount));</span><br><span class="line">                    <span class="built_in">string</span> newNum = str.substr(<span class="number">0</span>, str.find(<span class="string">&#x27;.&#x27;</span>) + <span class="number">3</span>);</span><br><span class="line">                    res +=  newNum;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    res += cur;</span><br><span class="line">                &#125;</span><br><span class="line">                i--;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                res += c;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2289-Steps-to-Make-Array-Non-decreasing"><a href="#Failed-2289-Steps-to-Make-Array-Non-decreasing" class="headerlink" title="Failed: 2289. Steps to Make Array Non-decreasing"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/steps-to-make-array-non-decreasing/">2289. Steps to Make Array Non-decreasing</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one step, <strong>remove</strong> all elements <code>nums[i]</code> where <code>nums[i - 1] &gt; nums[i]</code> for all <code>0 &lt; i &lt; nums.length</code>.</p>
<p>Return <em>the number of steps performed until</em> <code>nums</code> <em>becomes a <strong>non-decreasing</strong> array</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [5,3,4,4,7,3,6,11,8,5,11]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The following are the steps performed:</span><br><span class="line">- Step 1: [5,3,4,4,7,3,6,11,8,5,11] becomes [5,4,4,7,6,11,11]</span><br><span class="line">- Step 2: [5,4,4,7,6,11,11] becomes [5,4,7,11,11]</span><br><span class="line">- Step 3: [5,4,7,11,11] becomes [5,7,11,11]</span><br><span class="line">[5,7,11,11] is a non-decreasing array. Therefore, we return 3.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [4,5,7,7,13]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: nums is already a non-decreasing array. Therefore, we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
</ul>
<p>Medium，想到了最小栈和dp，但解决问题水平稍欠火候</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">totalSteps</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="built_in">stack</span>&lt;<span class="keyword">int</span>&gt; stk;</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">(n)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i&lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> cur = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">while</span> (!stk.empty() &amp;&amp; nums[i] &gt;= nums[stk.top()]) &#123;</span><br><span class="line">                cur = max(cur, dp[stk.top()]);</span><br><span class="line">                stk.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (!stk.empty()) &#123;</span><br><span class="line">                dp[i] = cur + <span class="number">1</span>;</span><br><span class="line">                res = max(res, cur + <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            stk.push(i);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2290-Minimum-Obstacle-Removal-to-Reach-Corner"><a href="#Failed-2290-Minimum-Obstacle-Removal-to-Reach-Corner" class="headerlink" title="Failed: 2290. Minimum Obstacle Removal to Reach Corner"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-obstacle-removal-to-reach-corner/">2290. Minimum Obstacle Removal to Reach Corner</a></h3><p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code>. Each cell has one of two values:</p>
<ul>
<li><code>0</code> represents an <strong>empty</strong> cell,</li>
<li><code>1</code> represents an <strong>obstacle</strong> that may be removed.</li>
</ul>
<p>You can move up, down, left, or right from and to an empty cell.</p>
<p>Return <em>the <strong>minimum</strong> number of <strong>obstacles</strong> to <strong>remove</strong> so you can move from the upper left corner</em> <code>(0, 0)</code> <em>to the lower right corner</em> <code>(m - 1, n - 1)</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529143135.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[0,1,1],[1,1,0],[1,1,0]]</span><br><span class="line">Output: 2</span><br><span class="line">Explanation: We can remove the obstacles at (0, 1) and (0, 2) to create a path from (0, 0) to (2, 2).</span><br><span class="line">It can be shown that we need to remove at least 2 obstacles, so we return 2.</span><br><span class="line">Note that there may be other ways to remove 2 obstacles to create a path.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220529143234.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: We can move from (0, 0) to (2, 4) without removing any obstacles, so we return 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 105</code></li>
<li><code>2 &lt;= m * n &lt;= 105</code></li>
<li><code>grid[i][j]</code> is either <code>0</code> <strong>or</strong> <code>1</code>.</li>
<li><code>grid[0][0] == grid[m - 1][n - 1] == 0</code></li>
</ul>
<p>Hard，动态规划+优先队列</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumObstacles</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m = grid.size(), n = grid[<span class="number">0</span>].size();</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; dp(m, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(n, <span class="number">1e5</span> + <span class="number">1</span>));</span><br><span class="line">        <span class="built_in">priority_queue</span>&lt;tuple&lt;<span class="keyword">int</span>, <span class="keyword">int</span>, <span class="keyword">int</span>&gt;, <span class="built_in">vector</span>&lt;tuple&lt;<span class="keyword">int</span>, <span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;, greater&lt;tuple&lt;<span class="keyword">int</span>, <span class="keyword">int</span>, <span class="keyword">int</span>&gt;&gt;&gt; pq;</span><br><span class="line">        </span><br><span class="line">        pq.push(&#123;grid[<span class="number">0</span>][<span class="number">0</span>], <span class="number">0</span>, <span class="number">0</span>&#125;);</span><br><span class="line">        <span class="keyword">int</span> dirs[<span class="number">4</span>][<span class="number">2</span>] = &#123;&#123;<span class="number">0</span>, <span class="number">1</span>&#125;,&#123;<span class="number">-1</span>, <span class="number">0</span>&#125;,&#123;<span class="number">1</span>, <span class="number">0</span>&#125;,&#123;<span class="number">0</span>, <span class="number">-1</span>&#125;&#125;;</span><br><span class="line">        <span class="keyword">while</span> (!pq.empty()) &#123;</span><br><span class="line">            <span class="keyword">auto</span> [c, i, j] = pq.top();</span><br><span class="line">            pq.pop();</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">if</span> (i == m - <span class="number">1</span> &amp;&amp; j == n - <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> c + grid[i][j];</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;d: dirs) &#123;</span><br><span class="line">                <span class="keyword">int</span> x = d[<span class="number">0</span>] + i;</span><br><span class="line">                <span class="keyword">int</span> y = d[<span class="number">1</span>] + j;</span><br><span class="line">                </span><br><span class="line">                <span class="keyword">if</span> (x &lt; <span class="number">0</span> || y &lt; <span class="number">0</span> || x &gt;= m || y &gt;= n)</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                </span><br><span class="line">                <span class="keyword">if</span> (dp[x][y] &gt; grid[x][y] + c) &#123;</span><br><span class="line">                    dp[x][y] = grid[x][y] + c;</span><br><span class="line">                    pq.push(&#123;grid[x][y] + c, x, y&#125;);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-296"><a href="#Weekly-Contest-296" class="headerlink" title="Weekly Contest 296"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-296/">Weekly Contest 296</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220605164056.png" alt="image-20220605164049910"></p>
<h3 id="2293-Min-Max-Game"><a href="#2293-Min-Max-Game" class="headerlink" title="2293. Min Max Game"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/min-max-game/">2293. Min Max Game</a></h3><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> whose length is a power of <code>2</code>.</p>
<p>Apply the following algorithm on <code>nums</code>:</p>
<ol>
<li>Let <code>n</code> be the length of <code>nums</code>. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n / 2</code>.</li>
<li>For every <strong>even</strong> index <code>i</code> where <code>0 &lt;= i &lt; n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>min(nums[2 * i], nums[2 * i + 1])</code>.</li>
<li>For every <strong>odd</strong> index <code>i</code> where <code>0 &lt;= i &lt; n / 2</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>max(nums[2 * i], nums[2 * i + 1])</code>.</li>
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
</ol>
<p>Return <em>the last number that remains in</em> <code>nums</code> <em>after applying the algorithm.</em></p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220605164419.png" alt="example1drawio"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,3,5,2,4,8,2,2]</span><br><span class="line">Output: 1</span><br><span class="line">Explanation: The following arrays are the results of applying the algorithm repeatedly.</span><br><span class="line">First: nums &#x3D; [1,5,4,2]</span><br><span class="line">Second: nums &#x3D; [1,4]</span><br><span class="line">Third: nums &#x3D; [1]</span><br><span class="line">1 is the last remaining number, so we return 1.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [3]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: 3 is already the last remaining number, so we return 3.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1024</code></li>
<li><code>1 &lt;= nums[i] &lt;= 109</code></li>
<li><code>nums.length</code> is a power of <code>2</code>.</li>
</ul>
<p>Easy，模拟法</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minMaxGame</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = nums.size();</span><br><span class="line">        <span class="keyword">while</span> (n != <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">bool</span> isMin = <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n / <span class="number">2</span>; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (isMin == <span class="literal">true</span>) &#123;</span><br><span class="line">                    nums[i] = min(nums[<span class="number">2</span> * i], nums[<span class="number">2</span> * i + <span class="number">1</span>]);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    nums[i] = max(nums[<span class="number">2</span> * i], nums[<span class="number">2</span> * i + <span class="number">1</span>]);</span><br><span class="line">                &#125;</span><br><span class="line">                isMin = !isMin;</span><br><span class="line">            &#125;</span><br><span class="line">            n /= <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> nums[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2294-Partition-Array-Such-That-Maximum-Difference-Is-K"><a href="#2294-Partition-Array-Such-That-Maximum-Difference-Is-K" class="headerlink" title="2294. Partition Array Such That Maximum Difference Is K"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/">2294. Partition Array Such That Maximum Difference Is K</a></h3><p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p>
<p>Return <em>the <strong>minimum</strong> number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong></em> <code>k</code><em>.</em></p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [3,6,1,2,5], k &#x3D; 2</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">We can partition nums into the two subsequences [3,1,2] and [6,5].</span><br><span class="line">The difference between the maximum and minimum value in the first subsequence is 3 - 1 &#x3D; 2.</span><br><span class="line">The difference between the maximum and minimum value in the second subsequence is 6 - 5 &#x3D; 1.</span><br><span class="line">Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,3], k &#x3D; 1</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">We can partition nums into the two subsequences [1,2] and [3].</span><br><span class="line">The difference between the maximum and minimum value in the first subsequence is 2 - 1 &#x3D; 1.</span><br><span class="line">The difference between the maximum and minimum value in the second subsequence is 3 - 3 &#x3D; 0.</span><br><span class="line">Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [2,2,4,5], k &#x3D; 0</span><br><span class="line">Output: 3</span><br><span class="line">Explanation:</span><br><span class="line">We can partition nums into the three subsequences [2,2], [4], and [5].</span><br><span class="line">The difference between the maximum and minimum value in the first subsequences is 2 - 2 &#x3D; 0.</span><br><span class="line">The difference between the maximum and minimum value in the second subsequences is 4 - 4 &#x3D; 0.</span><br><span class="line">The difference between the maximum and minimum value in the third subsequences is 5 - 5 &#x3D; 0.</span><br><span class="line">Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 105</code></li>
<li><code>0 &lt;= nums[i] &lt;= 105</code></li>
<li><code>0 &lt;= k &lt;= 105</code></li>
</ul>
<p>Medium，排序+贪心</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">partitionArray</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        sort(nums.begin(), nums.end());</span><br><span class="line">        <span class="keyword">int</span> pre = <span class="number">0</span>, ans = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (nums[i] - nums[pre] &gt; k) &#123;</span><br><span class="line">                pre = i;</span><br><span class="line">                ans++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2295-Replace-Elements-in-an-Array"><a href="#2295-Replace-Elements-in-an-Array" class="headerlink" title="2295. Replace Elements in an Array"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/replace-elements-in-an-array/">2295. Replace Elements in an Array</a></h3><p>You are given a <strong>0-indexed</strong> array <code>nums</code> that consists of <code>n</code> <strong>distinct</strong> positive integers. Apply <code>m</code> operations to this array, where in the <code>ith</code> operation you replace the number <code>operations[i][0]</code> with <code>operations[i][1]</code>.</p>
<p>It is guaranteed that in the <code>ith</code> operation:</p>
<ul>
<li><code>operations[i][0]</code> <strong>exists</strong> in <code>nums</code>.</li>
<li><code>operations[i][1]</code> does <strong>not</strong> exist in <code>nums</code>.</li>
</ul>
<p>Return <em>the array obtained after applying all the operations</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2,4,6], operations &#x3D; [[1,3],[4,7],[6,1]]</span><br><span class="line">Output: [3,2,7,1]</span><br><span class="line">Explanation: We perform the following operations on nums:</span><br><span class="line">- Replace the number 1 with 3. nums becomes [3,2,4,6].</span><br><span class="line">- Replace the number 4 with 7. nums becomes [3,2,7,6].</span><br><span class="line">- Replace the number 6 with 1. nums becomes [3,2,7,1].</span><br><span class="line">We return the final array [3,2,7,1].</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,2], operations &#x3D; [[1,3],[2,1],[3,2]]</span><br><span class="line">Output: [2,1]</span><br><span class="line">Explanation: We perform the following operations to nums:</span><br><span class="line">- Replace the number 1 with 3. nums becomes [3,2].</span><br><span class="line">- Replace the number 2 with 1. nums becomes [3,1].</span><br><span class="line">- Replace the number 3 with 2. nums becomes [2,1].</span><br><span class="line">We return the array [2,1].</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>m == operations.length</code></li>
<li><code>1 &lt;= n, m &lt;= 105</code></li>
<li>All the values of <code>nums</code> are <strong>distinct</strong>.</li>
<li><code>operations[i].length == 2</code></li>
<li><code>1 &lt;= nums[i], operations[i][0], operations[i][1] &lt;= 106</code></li>
<li><code>operations[i][0]</code> will exist in <code>nums</code> when applying the <code>ith</code> operation.</li>
<li><code>operations[i][1]</code> will not exist in <code>nums</code> when applying the <code>ith</code> operation.</li>
</ul>
<p>Medium，map保存数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">arrayChange</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; operations)</span> </span>&#123;</span><br><span class="line">        <span class="built_in">unordered_map</span>&lt;<span class="keyword">int</span>, <span class="keyword">int</span>&gt; numToIndex;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.size(); i++) &#123;</span><br><span class="line">            numToIndex[nums[i]] = i;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; operations.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> deletedNum = operations[i][<span class="number">0</span>], replacedNum = operations[i][<span class="number">1</span>];</span><br><span class="line">            <span class="keyword">int</span> index = numToIndex[deletedNum];</span><br><span class="line">            numToIndex.erase(deletedNum);</span><br><span class="line">            nums[index] = replacedNum;</span><br><span class="line">            numToIndex[replacedNum] = index;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> nums;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2296-Design-a-Text-Editor"><a href="#Failed-2296-Design-a-Text-Editor" class="headerlink" title="Failed: 2296. Design a Text Editor"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/design-a-text-editor/">2296. Design a Text Editor</a></h3><p>Design a text editor with a cursor that can do the following:</p>
<ul>
<li><strong>Add</strong> text to where the cursor is.</li>
<li><strong>Delete</strong> text from where the cursor is (simulating the backspace key).</li>
<li><strong>Move</strong> the cursor either left or right.</li>
</ul>
<p>When deleting text, only characters to the left of the cursor will be deleted. The cursor will also remain within the actual text and cannot be moved beyond it. More formally, we have that <code>0 &lt;= cursor.position &lt;= currentText.length</code> always holds.</p>
<p>Implement the <code>TextEditor</code> class:</p>
<ul>
<li><code>TextEditor()</code> Initializes the object with empty text.</li>
<li><code>void addText(string text)</code> Appends <code>text</code> to where the cursor is. The cursor ends to the right of <code>text</code>.</li>
<li><code>int deleteText(int k)</code> Deletes <code>k</code> characters to the left of the cursor. Returns the number of characters actually deleted.</li>
<li><code>string cursorLeft(int k)</code> Moves the cursor to the left <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
<li><code>string cursorRight(int k)</code> Moves the cursor to the right <code>k</code> times. Returns the last <code>min(10, len)</code> characters to the left of the cursor, where <code>len</code> is the number of characters to the left of the cursor.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">Input</span><br><span class="line">[&quot;TextEditor&quot;, &quot;addText&quot;, &quot;deleteText&quot;, &quot;addText&quot;, &quot;cursorRight&quot;, &quot;cursorLeft&quot;, &quot;deleteText&quot;, &quot;cursorLeft&quot;, &quot;cursorRight&quot;]</span><br><span class="line">[[], [&quot;leetcode&quot;], [4], [&quot;practice&quot;], [3], [8], [10], [2], [6]]</span><br><span class="line">Output</span><br><span class="line">[null, null, 4, null, &quot;etpractice&quot;, &quot;leet&quot;, 4, &quot;&quot;, &quot;practi&quot;]</span><br><span class="line"></span><br><span class="line">Explanation</span><br><span class="line">TextEditor textEditor &#x3D; new TextEditor(); &#x2F;&#x2F; The current text is &quot;|&quot;. (The &#39;|&#39; character represents the cursor)</span><br><span class="line">textEditor.addText(&quot;leetcode&quot;); &#x2F;&#x2F; The current text is &quot;leetcode|&quot;.</span><br><span class="line">textEditor.deleteText(4); &#x2F;&#x2F; return 4</span><br><span class="line">                          &#x2F;&#x2F; The current text is &quot;leet|&quot;. </span><br><span class="line">                          &#x2F;&#x2F; 4 characters were deleted.</span><br><span class="line">textEditor.addText(&quot;practice&quot;); &#x2F;&#x2F; The current text is &quot;leetpractice|&quot;. </span><br><span class="line">textEditor.cursorRight(3); &#x2F;&#x2F; return &quot;etpractice&quot;</span><br><span class="line">                           &#x2F;&#x2F; The current text is &quot;leetpractice|&quot;. </span><br><span class="line">                           &#x2F;&#x2F; The cursor cannot be moved beyond the actual text and thus did not move.</span><br><span class="line">                           &#x2F;&#x2F; &quot;etpractice&quot; is the last 10 characters to the left of the cursor.</span><br><span class="line">textEditor.cursorLeft(8); &#x2F;&#x2F; return &quot;leet&quot;</span><br><span class="line">                          &#x2F;&#x2F; The current text is &quot;leet|practice&quot;.</span><br><span class="line">                          &#x2F;&#x2F; &quot;leet&quot; is the last min(10, 4) &#x3D; 4 characters to the left of the cursor.</span><br><span class="line">textEditor.deleteText(10); &#x2F;&#x2F; return 4</span><br><span class="line">                           &#x2F;&#x2F; The current text is &quot;|practice&quot;.</span><br><span class="line">                           &#x2F;&#x2F; Only 4 characters were deleted.</span><br><span class="line">textEditor.cursorLeft(2); &#x2F;&#x2F; return &quot;&quot;</span><br><span class="line">                          &#x2F;&#x2F; The current text is &quot;|practice&quot;.</span><br><span class="line">                          &#x2F;&#x2F; The cursor cannot be moved beyond the actual text and thus did not move. </span><br><span class="line">                          &#x2F;&#x2F; &quot;&quot; is the last min(10, 0) &#x3D; 0 characters to the left of the cursor.</span><br><span class="line">textEditor.cursorRight(6); &#x2F;&#x2F; return &quot;practi&quot;</span><br><span class="line">                           &#x2F;&#x2F; The current text is &quot;practi|ce&quot;.</span><br><span class="line">                           &#x2F;&#x2F; &quot;practi&quot; is the last min(10, 6) &#x3D; 6 characters to the left of the cursor.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= text.length, k &lt;= 40</code></li>
<li><code>text</code> consists of lowercase English letters.</li>
<li>At most <code>2 * 104</code> calls <strong>in total</strong> will be made to <code>addText</code>, <code>deleteText</code>, <code>cursorLeft</code> and <code>cursorRight</code>.</li>
</ul>
<p>Hard，其实不难，莫名其妙++a换成a++就会超时</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TextEditor</span> &#123;</span></span><br><span class="line">    <span class="built_in">string</span> s;</span><br><span class="line">    <span class="keyword">int</span> pos ;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    TextEditor() &#123;</span><br><span class="line">        pos = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">addText</span><span class="params">(<span class="built_in">string</span> text)</span> </span>&#123;</span><br><span class="line">        s.insert(pos, text);</span><br><span class="line">        pos += text.size();</span><br><span class="line">        <span class="comment">// cout &lt;&lt; &quot;add&quot; &lt;&lt; endl;</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">deleteText</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (pos != <span class="number">0</span> &amp;&amp; ans &lt; k) &#123;</span><br><span class="line">            s.erase(pos - <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line">            pos--;</span><br><span class="line">            ans++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// cout &lt;&lt; &quot;delet&quot; &lt;&lt; endl;</span></span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">cursorLeft</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// cout &lt;&lt; &quot;r&quot; &lt;&lt; endl;</span></span><br><span class="line">        pos = max(pos - k, <span class="number">0</span>);</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = pos - <span class="number">10</span>; i &lt; pos; ++i) <span class="keyword">if</span> (i &gt;= <span class="number">0</span>) res += s[i];</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">cursorRight</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// cout &lt;&lt; &quot;R&quot; &lt;&lt; endl;</span></span><br><span class="line">        pos = min(pos+k, <span class="keyword">int</span>(s.size()));</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = pos - <span class="number">10</span>; i &lt; pos; ++i) <span class="keyword">if</span> (i &gt;= <span class="number">0</span>) res += s[i];</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Your TextEditor object will be instantiated and called as such:</span></span><br><span class="line"><span class="comment"> * TextEditor* obj = new TextEditor();</span></span><br><span class="line"><span class="comment"> * obj-&gt;addText(text);</span></span><br><span class="line"><span class="comment"> * int param_2 = obj-&gt;deleteText(k);</span></span><br><span class="line"><span class="comment"> * string param_3 = obj-&gt;cursorLeft(k);</span></span><br><span class="line"><span class="comment"> * string param_4 = obj-&gt;cursorRight(k);</span></span><br><span class="line"><span class="comment"> */</span></span><br></pre></td></tr></table></figure>

<p>使用栈</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TextEditor</span> &#123;</span></span><br><span class="line">    <span class="built_in">stack</span>&lt;<span class="keyword">char</span>&gt; left;</span><br><span class="line">    <span class="built_in">stack</span>&lt;<span class="keyword">char</span>&gt; right;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    TextEditor() &#123;</span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">addText</span><span class="params">(<span class="built_in">string</span> text)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">auto</span> &amp;c : text)&#123;</span><br><span class="line">            left.push(c);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">deleteText</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> cnt=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span>(!left.empty() <span class="keyword">and</span> k&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            left.pop();</span><br><span class="line">            cnt++;</span><br><span class="line">            k--;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> cnt;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">cursorLeft</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span>(!left.empty() <span class="keyword">and</span> k&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">char</span> c = left.top();left.pop();</span><br><span class="line">            right.push(c);</span><br><span class="line">            k--;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">// returning the last min(10, len) characters to the left of the cursor</span></span><br><span class="line">        <span class="keyword">return</span> cursorShiftString();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">cursorRight</span><span class="params">(<span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span>(!right.empty() <span class="keyword">and</span> k&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">char</span> c = right.top();right.pop();</span><br><span class="line">            left.push(c);</span><br><span class="line">            k--;</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="comment">// returning the last min(10, len) characters to the left of the cursor</span></span><br><span class="line">        <span class="keyword">return</span> cursorShiftString();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">	<span class="comment">// function to return the last min(10, len) characters to the left of the cursor</span></span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">cursorShiftString</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="built_in">string</span> rtn = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">int</span> cnt=<span class="number">10</span>;</span><br><span class="line">        <span class="keyword">while</span>(!left.empty() <span class="keyword">and</span> cnt&gt;<span class="number">0</span>)&#123;</span><br><span class="line">            <span class="keyword">char</span> c = left.top();left.pop();</span><br><span class="line">            rtn += c;</span><br><span class="line">            cnt--;</span><br><span class="line">        &#125;</span><br><span class="line">        reverse(rtn.begin(),rtn.end());</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;rtn.size();i++)&#123;</span><br><span class="line">            left.push(rtn[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> rtn;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-297"><a href="#Weekly-Contest-297" class="headerlink" title="Weekly Contest 297"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-297/">Weekly Contest 297</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220612180756.png" alt="image-20220612180750249"></p>
<h3 id="2303-Calculate-Amount-Paid-in-Taxes"><a href="#2303-Calculate-Amount-Paid-in-Taxes" class="headerlink" title="2303. Calculate Amount Paid in Taxes"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/calculate-amount-paid-in-taxes/">2303. Calculate Amount Paid in Taxes</a></h3><p>You are given a <strong>0-indexed</strong> 2D integer array <code>brackets</code> where <code>brackets[i] = [upperi, percenti]</code> means that the <code>ith</code> tax bracket has an upper bound of <code>upperi</code> and is taxed at a rate of <code>percenti</code>. The brackets are <strong>sorted</strong> by upper bound (i.e. <code>upperi-1 &lt; upperi</code> for <code>0 &lt; i &lt; brackets.length</code>).</p>
<p>Tax is calculated as follows:</p>
<ul>
<li>The first <code>upper0</code> dollars earned are taxed at a rate of <code>percent0</code>.</li>
<li>The next <code>upper1 - upper0</code> dollars earned are taxed at a rate of <code>percent1</code>.</li>
<li>The next <code>upper2 - upper1</code> dollars earned are taxed at a rate of <code>percent2</code>.</li>
<li>And so on.</li>
</ul>
<p>You are given an integer <code>income</code> representing the amount of money you earned. Return <em>the amount of money that you have to pay in taxes.</em> Answers within <code>10-5</code> of the actual answer will be accepted.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: brackets &#x3D; [[3,50],[7,10],[12,25]], income &#x3D; 10</span><br><span class="line">Output: 2.65000</span><br><span class="line">Explanation:</span><br><span class="line">The first 3 dollars you earn are taxed at 50%. You have to pay $3 * 50% &#x3D; $1.50 dollars in taxes.</span><br><span class="line">The next 7 - 3 &#x3D; 4 dollars you earn are taxed at 10%. You have to pay $4 * 10% &#x3D; $0.40 dollars in taxes.</span><br><span class="line">The final 10 - 7 &#x3D; 3 dollars you earn are taxed at 25%. You have to pay $3 * 25% &#x3D; $0.75 dollars in taxes.</span><br><span class="line">You have to pay a total of $1.50 + $0.40 + $0.75 &#x3D; $2.65 dollars in taxes.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: brackets &#x3D; [[1,0],[4,25],[5,50]], income &#x3D; 2</span><br><span class="line">Output: 0.25000</span><br><span class="line">Explanation:</span><br><span class="line">The first dollar you earn is taxed at 0%. You have to pay $1 * 0% &#x3D; $0 dollars in taxes.</span><br><span class="line">The second dollar you earn is taxed at 25%. You have to pay $1 * 25% &#x3D; $0.25 dollars in taxes.</span><br><span class="line">You have to pay a total of $0 + $0.25 &#x3D; $0.25 dollars in taxes.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: brackets &#x3D; [[2,50]], income &#x3D; 0</span><br><span class="line">Output: 0.00000</span><br><span class="line">Explanation:</span><br><span class="line">You have no income to tax, so you have to pay a total of $0 dollars in taxes.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= brackets.length &lt;= 100</code></li>
<li><code>1 &lt;= upperi &lt;= 1000</code></li>
<li><code>0 &lt;= percenti &lt;= 100</code></li>
<li><code>0 &lt;= income &lt;= 1000</code></li>
<li><code>upperi</code> is sorted in ascending order.</li>
<li>All the values of <code>upperi</code> are <strong>unique</strong>.</li>
<li>The upper bound of the last tax bracket is greater than or equal to <code>income</code>.</li>
</ul>
<p>Easy，累进税制计算</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">double</span> <span class="title">calculateTax</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; brackets, <span class="keyword">int</span> income)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">double</span> res = <span class="number">0.0</span>;</span><br><span class="line">        <span class="keyword">int</span> pre = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> &amp;bracket: brackets) &#123;</span><br><span class="line">            <span class="keyword">if</span> (income &lt;= bracket[<span class="number">0</span>]) &#123;</span><br><span class="line">                res += (<span class="keyword">double</span>)(income - pre) * bracket[<span class="number">1</span>] /<span class="number">100</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            res += (<span class="keyword">double</span>)(bracket[<span class="number">0</span>] - pre) * bracket[<span class="number">1</span>] /<span class="number">100</span>;</span><br><span class="line">            pre = bracket[<span class="number">0</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2304-Minimum-Path-Cost-in-a-Grid"><a href="#2304-Minimum-Path-Cost-in-a-Grid" class="headerlink" title="2304. Minimum Path Cost in a Grid"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-path-cost-in-a-grid/">2304. Minimum Path Cost in a Grid</a></h3><p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code> consisting of <strong>distinct</strong> integers from <code>0</code> to <code>m * n - 1</code>. You can move in this matrix from a cell to any other cell in the <strong>next</strong> row. That is, if you are in cell <code>(x, y)</code> such that <code>x &lt; m - 1</code>, you can move to any of the cells <code>(x + 1, 0)</code>, <code>(x + 1, 1)</code>, …, <code>(x + 1, n - 1)</code>. <strong>Note</strong> that it is not possible to move from cells in the last row.</p>
<p>Each possible move has a cost given by a <strong>0-indexed</strong> 2D array <code>moveCost</code> of size <code>(m * n) x n</code>, where <code>moveCost[i][j]</code> is the cost of moving from a cell with value <code>i</code> to a cell in column <code>j</code> of the next row. The cost of moving from cells in the last row of <code>grid</code> can be ignored.</p>
<p>The cost of a path in <code>grid</code> is the <strong>sum</strong> of all values of cells visited plus the <strong>sum</strong> of costs of all the moves made. Return <em>the <strong>minimum</strong> cost of a path that starts from any cell in the <strong>first</strong> row and ends at any cell in the <strong>last</strong> row.</em></p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220612181238.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[5,3],[4,0],[2,1]], moveCost &#x3D; [[9,8],[1,5],[10,12],[18,6],[2,4],[14,3]]</span><br><span class="line">Output: 17</span><br><span class="line">Explanation: The path with the minimum possible cost is the path 5 -&gt; 0 -&gt; 1.</span><br><span class="line">- The sum of the values of cells visited is 5 + 0 + 1 &#x3D; 6.</span><br><span class="line">- The cost of moving from 5 to 0 is 3.</span><br><span class="line">- The cost of moving from 0 to 1 is 8.</span><br><span class="line">So the total cost of the path is 6 + 3 + 8 &#x3D; 17.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[5,1,2],[4,0,3]], moveCost &#x3D; [[12,10,15],[20,23,8],[21,7,1],[8,1,13],[9,10,25],[5,3,2]]</span><br><span class="line">Output: 6</span><br><span class="line">Explanation: The path with the minimum possible cost is the path 2 -&gt; 3.</span><br><span class="line">- The sum of the values of cells visited is 2 + 3 &#x3D; 5.</span><br><span class="line">- The cost of moving from 2 to 3 is 1.</span><br><span class="line">So the total cost of this path is 5 + 1 &#x3D; 6.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>2 &lt;= m, n &lt;= 50</code></li>
<li><code>grid</code> consists of distinct integers from <code>0</code> to <code>m * n - 1</code>.</li>
<li><code>moveCost.length == m * n</code></li>
<li><code>moveCost[i].length == n</code></li>
<li><code>1 &lt;= moveCost[i][j] &lt;= 100</code></li>
</ul>
<p>Medium，dp保存上一行的最小值</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minPathCost</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; moveCost)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m = grid.size(), n = grid[<span class="number">0</span>].size();</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; cost = grid[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; m; i++) &#123;</span><br><span class="line">            <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">cur</span><span class="params">(n, INT_MAX)</span></span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; n; k++) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (cost[j] + moveCost[grid[i - <span class="number">1</span>][j]][k] + grid[i][k] &lt; cur[k]) &#123;</span><br><span class="line">                        cur[k] = cost[j] + moveCost[grid[i - <span class="number">1</span>][j]][k] + grid[i][k];</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            cost = cur;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = cost[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; n; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (res &gt; cost[j]) &#123;</span><br><span class="line">                res = cost[j];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2305-Fair-Distribution-of-Cookies"><a href="#2305-Fair-Distribution-of-Cookies" class="headerlink" title="2305. Fair Distribution of Cookies"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/fair-distribution-of-cookies/">2305. Fair Distribution of Cookies</a></h3><p>You are given an integer array <code>cookies</code>, where <code>cookies[i]</code> denotes the number of cookies in the <code>ith</code> bag. You are also given an integer <code>k</code> that denotes the number of children to distribute <strong>all</strong> the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.</p>
<p>The <strong>unfairness</strong> of a distribution is defined as the <strong>maximum</strong> <strong>total</strong> cookies obtained by a single child in the distribution.</p>
<p>Return <em>the <strong>minimum</strong> unfairness of all distributions</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: cookies &#x3D; [8,15,10,20,8], k &#x3D; 2</span><br><span class="line">Output: 31</span><br><span class="line">Explanation: One optimal distribution is [8,15,8] and [10,20]</span><br><span class="line">- The 1st child receives [8,15,8] which has a total of 8 + 15 + 8 &#x3D; 31 cookies.</span><br><span class="line">- The 2nd child receives [10,20] which has a total of 10 + 20 &#x3D; 30 cookies.</span><br><span class="line">The unfairness of the distribution is max(31,30) &#x3D; 31.</span><br><span class="line">It can be shown that there is no distribution with an unfairness less than 31.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: cookies &#x3D; [6,1,3,2,2,4,1,2], k &#x3D; 3</span><br><span class="line">Output: 7</span><br><span class="line">Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2]</span><br><span class="line">- The 1st child receives [6,1] which has a total of 6 + 1 &#x3D; 7 cookies.</span><br><span class="line">- The 2nd child receives [3,2,2] which has a total of 3 + 2 + 2 &#x3D; 7 cookies.</span><br><span class="line">- The 3rd child receives [4,1,2] which has a total of 4 + 1 + 2 &#x3D; 7 cookies.</span><br><span class="line">The unfairness of the distribution is max(7,7,7) &#x3D; 7.</span><br><span class="line">It can be shown that there is no distribution with an unfairness less than 7.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= cookies.length &lt;= 8</code></li>
<li><code>1 &lt;= cookies[i] &lt;= 105</code></li>
<li><code>2 &lt;= k &lt;= cookies.length</code></li>
</ul>
<p>Medium，dfs回溯</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">distributeCookies</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; cookies, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = cookies.size(), res = INT_MAX;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">count</span><span class="params">(k, <span class="number">0</span>)</span></span>;</span><br><span class="line">        dfs(cookies, count, <span class="number">0</span>, n, k, res);</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">dfs</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; cookies, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; count, <span class="keyword">int</span> index, <span class="keyword">int</span> n, <span class="keyword">int</span> k, <span class="keyword">int</span> &amp;res)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (index == n) &#123;</span><br><span class="line">            <span class="keyword">int</span> maxNum = count[<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; k; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (count[i] &gt; maxNum) &#123;</span><br><span class="line">                    maxNum = count[i];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (maxNum &lt; res) &#123;</span><br><span class="line">                res = maxNum;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; k; i++) &#123;</span><br><span class="line">            count[i] += cookies[index];</span><br><span class="line">            dfs(cookies, count, index + <span class="number">1</span>, n, k, res);</span><br><span class="line">            count[i] -= cookies[index];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2306-Naming-a-Company"><a href="#Failed-2306-Naming-a-Company" class="headerlink" title="Failed: 2306. Naming a Company"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/naming-a-company/">2306. Naming a Company</a></h3><p>You are given an array of strings <code>ideas</code> that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:</p>
<ol>
<li>Choose 2 <strong>distinct</strong> names from <code>ideas</code>, call them <code>ideaA</code> and <code>ideaB</code>.</li>
<li>Swap the first letters of <code>ideaA</code> and <code>ideaB</code> with each other.</li>
<li>If <strong>both</strong> of the new names are not found in the original <code>ideas</code>, then the name <code>ideaA ideaB</code> (the <strong>concatenation</strong> of <code>ideaA</code> and <code>ideaB</code>, separated by a space) is a valid company name.</li>
<li>Otherwise, it is not a valid name.</li>
</ol>
<p>Return <em>the number of <strong>distinct</strong> valid names for the company</em>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">Input: ideas &#x3D; [&quot;coffee&quot;,&quot;donuts&quot;,&quot;time&quot;,&quot;toffee&quot;]</span><br><span class="line">Output: 6</span><br><span class="line">Explanation: The following selections are valid:</span><br><span class="line">- (&quot;coffee&quot;, &quot;donuts&quot;): The company name created is &quot;doffee conuts&quot;.</span><br><span class="line">- (&quot;donuts&quot;, &quot;coffee&quot;): The company name created is &quot;conuts doffee&quot;.</span><br><span class="line">- (&quot;donuts&quot;, &quot;time&quot;): The company name created is &quot;tonuts dime&quot;.</span><br><span class="line">- (&quot;donuts&quot;, &quot;toffee&quot;): The company name created is &quot;tonuts doffee&quot;.</span><br><span class="line">- (&quot;time&quot;, &quot;donuts&quot;): The company name created is &quot;dime tonuts&quot;.</span><br><span class="line">- (&quot;toffee&quot;, &quot;donuts&quot;): The company name created is &quot;doffee tonuts&quot;.</span><br><span class="line">Therefore, there are a total of 6 distinct company names.</span><br><span class="line"></span><br><span class="line">The following are some examples of invalid selections:</span><br><span class="line">- (&quot;coffee&quot;, &quot;time&quot;): The name &quot;toffee&quot; formed after swapping already exists in the original array.</span><br><span class="line">- (&quot;time&quot;, &quot;toffee&quot;): Both names are still the same after swapping and exist in the original array.</span><br><span class="line">- (&quot;coffee&quot;, &quot;toffee&quot;): Both names formed after swapping already exist in the original array.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: ideas &#x3D; [&quot;lack&quot;,&quot;back&quot;]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: There are no valid selections. Therefore, 0 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= ideas.length &lt;= 5 * 104</code></li>
<li><code>1 &lt;= ideas[i].length &lt;= 10</code></li>
<li><code>ideas[i]</code> consists of lowercase English letters.</li>
<li>All the strings in <code>ideas</code> are <strong>unique</strong>.</li>
</ul>
<p>Hard，保存首字母对应的单词</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">distinctNames</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">string</span>&gt;&amp; ideas)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="function"><span class="built_in">unordered_set</span>&lt;<span class="built_in">string</span>&gt; <span class="title">s</span><span class="params">(begin(ideas), end(ideas))</span></span>;</span><br><span class="line">        <span class="keyword">int</span> cnt[<span class="number">26</span>][<span class="number">26</span>] = &#123;&#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">auto</span> i = <span class="number">0</span>; i &lt; ideas.size(); ++i) &#123;</span><br><span class="line">            <span class="keyword">char</span> first = ideas[i][<span class="number">0</span>];</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">char</span> ch = <span class="string">&#x27;a&#x27;</span>; ch &lt;= <span class="string">&#x27;z&#x27;</span>; ++ch) &#123;</span><br><span class="line">                ideas[i][<span class="number">0</span>] = ch;</span><br><span class="line">                cnt[ch - <span class="string">&#x27;a&#x27;</span>][first - <span class="string">&#x27;a&#x27;</span>] += s.count(ideas[i]) == <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">26</span>; i++)</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; <span class="number">26</span>; j++)</span><br><span class="line">                res += cnt[i][j] * cnt[j][i];</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-298"><a href="#Weekly-Contest-298" class="headerlink" title="Weekly Contest 298"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-298/">Weekly Contest 298</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619175907.png" alt="image-20220619175901428"></p>
<h3 id="2309-Greatest-English-Letter-in-Upper-and-Lower-Case"><a href="#2309-Greatest-English-Letter-in-Upper-and-Lower-Case" class="headerlink" title="2309. Greatest English Letter in Upper and Lower Case"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/">2309. Greatest English Letter in Upper and Lower Case</a></h3><p>Given a string of English letters <code>s</code>, return <em>the <strong>greatest</strong> English letter which occurs as <strong>both</strong> a lowercase and uppercase letter in</em> <code>s</code>. The returned letter should be in <strong>uppercase</strong>. If no such letter exists, return <em>an empty string</em>.</p>
<p>An English letter <code>b</code> is <strong>greater</strong> than another letter <code>a</code> if <code>b</code> appears <strong>after</strong> <code>a</code> in the English alphabet.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;lEeTcOdE&quot;</span><br><span class="line">Output: &quot;E&quot;</span><br><span class="line">Explanation:</span><br><span class="line">The letter &#39;E&#39; is the only letter to appear in both lower and upper case.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;arRAzFif&quot;</span><br><span class="line">Output: &quot;R&quot;</span><br><span class="line">Explanation:</span><br><span class="line">The letter &#39;R&#39; is the greatest letter to appear in both lower and upper case.</span><br><span class="line">Note that &#39;A&#39; and &#39;F&#39; also appear in both lower and upper case, but &#39;R&#39; is greater than &#39;F&#39; or &#39;A&#39;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;AbCdEfGhIjK&quot;</span><br><span class="line">Output: &quot;&quot;</span><br><span class="line">Explanation:</span><br><span class="line">There is no letter that appears in both lower and upper case.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s</code> consists of lowercase and uppercase English letters.</li>
</ul>
<p>Easy，统计各字母</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">greatestLetter</span><span class="params">(<span class="built_in">string</span> s)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">isUpperExisted</span><span class="params">(<span class="number">26</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; <span class="title">isLowerExisted</span><span class="params">(<span class="number">26</span>, <span class="literal">false</span>)</span></span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: s) &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="string">&#x27;a&#x27;</span> &lt;= c &amp;&amp; c &lt;= <span class="string">&#x27;z&#x27;</span>) &#123;</span><br><span class="line">                isLowerExisted[c - <span class="string">&#x27;a&#x27;</span>] = <span class="literal">true</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                isUpperExisted[c - <span class="string">&#x27;A&#x27;</span>] = <span class="literal">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">string</span> res;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">25</span>; i &gt;= <span class="number">0</span>; i--) &#123;</span><br><span class="line">            <span class="keyword">if</span> (isUpperExisted[i] &amp;&amp; isLowerExisted[i]) &#123;</span><br><span class="line">                res += <span class="string">&#x27;A&#x27;</span> + i;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2310-Sum-of-Numbers-With-Units-Digit-K"><a href="#2310-Sum-of-Numbers-With-Units-Digit-K" class="headerlink" title="2310. Sum of Numbers With Units Digit K"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/">2310. Sum of Numbers With Units Digit K</a></h3><p>Given two integers <code>num</code> and <code>k</code>, consider a set of positive integers with the following properties:</p>
<ul>
<li>The units digit of each integer is <code>k</code>.</li>
<li>The sum of the integers is <code>num</code>.</li>
</ul>
<p>Return <em>the <strong>minimum</strong> possible size of such a set, or</em> <code>-1</code> <em>if no such set exists.</em></p>
<p>Note:</p>
<ul>
<li>The set can contain multiple instances of the same integer, and the sum of an empty set is considered <code>0</code>.</li>
<li>The <strong>units digit</strong> of a number is the rightmost digit of the number.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; 58, k &#x3D; 9</span><br><span class="line">Output: 2</span><br><span class="line">Explanation:</span><br><span class="line">One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.</span><br><span class="line">Another valid set is [19,39].</span><br><span class="line">It can be shown that 2 is the minimum possible size of a valid set.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; 37, k &#x3D; 2</span><br><span class="line">Output: -1</span><br><span class="line">Explanation: It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: num &#x3D; 0, k &#x3D; 7</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: The sum of an empty set is considered 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= num &lt;= 3000</code></li>
<li><code>0 &lt;= k &lt;= 9</code></li>
</ul>
<p>Medium，数学计算未化简</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumNumbers</span><span class="params">(<span class="keyword">int</span> num, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num == <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">10</span> &amp;&amp; k * i &lt;= num; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> ((num - k * i) % <span class="number">10</span> == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2311-Longest-Binary-Subsequence-Less-Than-or-Equal-to-K"><a href="#Failed-2311-Longest-Binary-Subsequence-Less-Than-or-Equal-to-K" class="headerlink" title="Failed: 2311. Longest Binary Subsequence Less Than or Equal to K"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/">2311. Longest Binary Subsequence Less Than or Equal to K</a></h3><p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>
<p>Return <em>the length of the <strong>longest</strong> subsequence of</em> <code>s</code> <em>that makes up a <strong>binary</strong> number less than or equal to</em> <code>k</code>.</p>
<p>Note:</p>
<ul>
<li>The subsequence can contain <strong>leading zeroes</strong>.</li>
<li>The empty string is considered to be equal to <code>0</code>.</li>
<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
</ul>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;1001010&quot;, k &#x3D; 5</span><br><span class="line">Output: 5</span><br><span class="line">Explanation: The longest subsequence of s that makes up a binary number less than or equal to 5 is &quot;00010&quot;, as this number is equal to 2 in decimal.</span><br><span class="line">Note that &quot;00100&quot; and &quot;00101&quot; are also possible, which are equal to 4 and 5 in decimal, respectively.</span><br><span class="line">The length of this subsequence is 5, so 5 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: s &#x3D; &quot;00101001&quot;, k &#x3D; 1</span><br><span class="line">Output: 6</span><br><span class="line">Explanation: &quot;000001&quot; is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.</span><br><span class="line">The length of this subsequence is 6, so 6 is returned.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
<li><code>1 &lt;= k &lt;= 109</code></li>
</ul>
<p>Medium，动态规划当前结果不超过k的长度数组</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">longestSubsequence</span><span class="params">(<span class="built_in">string</span> s, <span class="keyword">int</span> k)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = s.size();</span><br><span class="line">        <span class="keyword">int</span> dp[<span class="number">1001</span>] = &#123;&#125;, j = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: s) &#123;</span><br><span class="line">            <span class="keyword">if</span> (dp[j] * <span class="number">2</span> + (c - <span class="string">&#x27;0&#x27;</span>) &lt;= k)</span><br><span class="line">                dp[++j] = dp[j] * <span class="number">2</span> + (c - <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = j; i &gt; <span class="number">0</span>; --i)</span><br><span class="line">                dp[i] = min(dp[i], dp[i - <span class="number">1</span>] * <span class="number">2</span> + c - <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> j;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2312-Selling-Pieces-of-Wood"><a href="#Failed-2312-Selling-Pieces-of-Wood" class="headerlink" title="Failed: 2312. Selling Pieces of Wood"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/selling-pieces-of-wood/">2312. Selling Pieces of Wood</a></h3><p>You are given two integers <code>m</code> and <code>n</code> that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array <code>prices</code>, where <code>prices[i] = [hi, wi, pricei]</code> indicates you can sell a rectangular piece of wood of height <code>hi</code> and width <code>wi</code> for <code>pricei</code> dollars.</p>
<p>To cut a piece of wood, you must make a vertical or horizontal cut across the <strong>entire</strong> height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to <code>prices</code>. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you <strong>cannot</strong> rotate a piece to swap its height and width.</p>
<p>Return <em>the <strong>maximum</strong> money you can earn after cutting an</em> <code>m x n</code> <em>piece of wood</em>.</p>
<p>Note that you can cut the piece of wood as many times as you want.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619194059.png" alt="ex1"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: m &#x3D; 3, n &#x3D; 5, prices &#x3D; [[1,4,2],[2,2,7],[2,1,3]]</span><br><span class="line">Output: 19</span><br><span class="line">Explanation: The diagram above shows a possible scenario. It consists of:</span><br><span class="line">- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 &#x3D; 14.</span><br><span class="line">- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 &#x3D; 3.</span><br><span class="line">- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 &#x3D; 2.</span><br><span class="line">This obtains a total of 14 + 3 + 2 &#x3D; 19 money earned.</span><br><span class="line">It can be shown that 19 is the maximum amount of money that can be earned.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220619194110.png" alt="ex2new"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: m &#x3D; 4, n &#x3D; 6, prices &#x3D; [[3,2,10],[1,4,2],[4,1,3]]</span><br><span class="line">Output: 32</span><br><span class="line">Explanation: The diagram above shows a possible scenario. It consists of:</span><br><span class="line">- 3 pieces of wood shaped 3 x 2, selling for a price of 3 * 10 &#x3D; 30.</span><br><span class="line">- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 &#x3D; 2.</span><br><span class="line">This obtains a total of 30 + 2 &#x3D; 32 money earned.</span><br><span class="line">It can be shown that 32 is the maximum amount of money that can be earned.</span><br><span class="line">Notice that we cannot rotate the 1 x 4 piece of wood to obtain a 4 x 1 piece of wood.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m, n &lt;= 200</code></li>
<li><code>1 &lt;= prices.length &lt;= 2 * 104</code></li>
<li><code>prices[i].length == 3</code></li>
<li><code>1 &lt;= hi &lt;= m</code></li>
<li><code>1 &lt;= wi &lt;= n</code></li>
<li><code>1 &lt;= pricei &lt;= 106</code></li>
<li>All the shapes of wood <code>(hi, wi)</code> are pairwise <strong>distinct</strong>.</li>
</ul>
<p>Hard，二维动态规划</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">class Solution &#123;</span><br><span class="line">public:</span><br><span class="line">    long long sellingWood(int m, int n, vector&lt;vector&lt;int&gt;&gt;&amp; prices) &#123;</span><br><span class="line">        long long dp[201][201] &#x3D; &#123;&#125;;</span><br><span class="line">        for (auto&amp; p: prices)</span><br><span class="line">            dp[p[0]][p[1]] &#x3D; p[2];</span><br><span class="line">        for (int w &#x3D; 1; w &lt;&#x3D; m; w++) &#123;</span><br><span class="line">            for (int h &#x3D; 1; h &lt;&#x3D; n; h++) &#123;</span><br><span class="line">                for (int a &#x3D; 1; a &lt;&#x3D; w &#x2F; 2; a++)</span><br><span class="line">                    dp[w][h] &#x3D; max(dp[w][h], dp[a][h] + dp[w - a][h]);</span><br><span class="line">                for (int a &#x3D; 1; a &lt;&#x3D; h &#x2F; 2; a++)</span><br><span class="line">                    dp[w][h] &#x3D; max(dp[w][h], dp[w][a] + dp[w][h - a]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return dp[m][n];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>





<h2 id="Weekly-Contest-299"><a href="#Weekly-Contest-299" class="headerlink" title="Weekly Contest 299"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-299/">Weekly Contest 299</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090612.png" alt="image-20220628090612532"></p>
<h3 id="2319-Check-if-Matrix-Is-X-Matrix"><a href="#2319-Check-if-Matrix-Is-X-Matrix" class="headerlink" title="2319. Check if Matrix Is X-Matrix"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/check-if-matrix-is-x-matrix/">2319. Check if Matrix Is X-Matrix</a></h3><p>A square matrix is said to be an <strong>X-Matrix</strong> if <strong>both</strong> of the following conditions hold:</p>
<ol>
<li>All the elements in the diagonals of the matrix are <strong>non-zero</strong>.</li>
<li>All other elements are 0.</li>
</ol>
<p>Given a 2D integer array <code>grid</code> of size <code>n x n</code> representing a square matrix, return <code>true</code> <em>if</em> <code>grid</code> <em>is an X-Matrix</em>. Otherwise, return <code>false</code>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090449.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]</span><br><span class="line">Output: true</span><br><span class="line">Explanation: Refer to the diagram above. </span><br><span class="line">An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.</span><br><span class="line">Thus, grid is an X-Matrix.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628090510.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[5,7,0],[0,3,1],[0,5,0]]</span><br><span class="line">Output: false</span><br><span class="line">Explanation: Refer to the diagram above.</span><br><span class="line">An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.</span><br><span class="line">Thus, grid is not an X-Matrix.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == grid.length == grid[i].length</code></li>
<li><code>3 &lt;= n &lt;= 100</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 105</code></li>
</ul>
<p>Easy，直接一个单元一个单元比较</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">bool</span> <span class="title">checkXMatrix</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = grid.size();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i == j || i + j == n - <span class="number">1</span>) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (grid[i][j] == <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (grid[i][j] != <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2320-Count-Number-of-Ways-to-Place-Houses"><a href="#2320-Count-Number-of-Ways-to-Place-Houses" class="headerlink" title="2320. Count Number of Ways to Place Houses"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/count-number-of-ways-to-place-houses/">2320. Count Number of Ways to Place Houses</a></h3><p>There is a street with <code>n * 2</code> <strong>plots</strong>, where there are <code>n</code> plots on each side of the street. The plots on each side are numbered from <code>1</code> to <code>n</code>. On each plot, a house can be placed.</p>
<p>Return <em>the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p>Note that if a house is placed on the <code>ith</code> plot on one side of the street, a house can also be placed on the <code>ith</code> plot on the other side of the street.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 1</span><br><span class="line">Output: 4</span><br><span class="line">Explanation: </span><br><span class="line">Possible arrangements:</span><br><span class="line">1. All plots are empty.</span><br><span class="line">2. A house is placed on one side of the street.</span><br><span class="line">3. A house is placed on the other side of the street.</span><br><span class="line">4. Two houses are placed, one on each side of the street.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220628091142.png" alt="image-20220628091142492"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 2</span><br><span class="line">Output: 9</span><br><span class="line">Explanation: The 9 possible arrangements are shown in the diagram above.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 104</code></li>
</ul>
<p>Medium，其实每边都是斐波拉契数列，好好思考</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countHousePlacements</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (n &lt; <span class="number">3</span>) <span class="keyword">return</span> (n + <span class="number">1</span>) * (n + <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line">        <span class="keyword">long</span> <span class="keyword">long</span> dp0 = <span class="number">2</span>, dp1 = <span class="number">3</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> temp = dp0 + dp1;</span><br><span class="line">            dp0 = dp1;</span><br><span class="line">            dp1 = temp % mod;</span><br><span class="line">        &#125;</span><br><span class="line">       </span><br><span class="line">        <span class="keyword">return</span> (dp1 * dp1) % mod;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2321-Maximum-Score-Of-Spliced-Array"><a href="#Failed-2321-Maximum-Score-Of-Spliced-Array" class="headerlink" title="Failed: 2321. Maximum Score Of Spliced Array"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/maximum-score-of-spliced-array/">2321. Maximum Score Of Spliced Array</a></h3><p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>
<p>You can choose two integers <code>left</code> and <code>right</code> where <code>0 &lt;= left &lt;= right &lt; n</code> and <strong>swap</strong> the subarray <code>nums1[left...right]</code> with the subarray <code>nums2[left...right]</code>.</p>
<ul>
<li>For example, if <code>nums1 = [1,2,3,4,5]</code> and <code>nums2 = [11,12,13,14,15]</code> and you choose <code>left = 1</code> and <code>right = 2</code>, <code>nums1</code> becomes <code>[1,**12,13**,4,5]</code> and <code>nums2</code> becomes <code>[11,**2,3**,14,15]</code>.</li>
</ul>
<p>You may choose to apply the mentioned operation <strong>once</strong> or not do anything.</p>
<p>The <strong>score</strong> of the arrays is the <strong>maximum</strong> of <code>sum(nums1)</code> and <code>sum(nums2)</code>, where <code>sum(arr)</code> is the sum of all the elements in the array <code>arr</code>.</p>
<p>Return <em>the <strong>maximum possible score</strong></em>.</p>
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array. <code>arr[left...right]</code> denotes the subarray that contains the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> (<strong>inclusive</strong>).</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums1 &#x3D; [60,60,60], nums2 &#x3D; [10,90,10]</span><br><span class="line">Output: 210</span><br><span class="line">Explanation: Choosing left &#x3D; 1 and right &#x3D; 1, we have nums1 &#x3D; [60,90,60] and nums2 &#x3D; [10,60,10].</span><br><span class="line">The score is max(sum(nums1), sum(nums2)) &#x3D; max(210, 80) &#x3D; 210.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums1 &#x3D; [20,40,20,70,30], nums2 &#x3D; [50,20,50,40,20]</span><br><span class="line">Output: 220</span><br><span class="line">Explanation: Choosing left &#x3D; 3, right &#x3D; 4, we have nums1 &#x3D; [20,40,20,40,20] and nums2 &#x3D; [50,20,50,70,30].</span><br><span class="line">The score is max(sum(nums1), sum(nums2)) &#x3D; max(140, 220) &#x3D; 220.</span><br></pre></td></tr></table></figure>

<p><strong>Example 3:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: nums1 &#x3D; [7,11,13], nums2 &#x3D; [1,1,1]</span><br><span class="line">Output: 31</span><br><span class="line">Explanation: We choose not to swap any subarray.</span><br><span class="line">The score is max(sum(nums1), sum(nums2)) &#x3D; max(31, 3) &#x3D; 31.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length == nums2.length</code></li>
<li><code>1 &lt;= n &lt;= 105</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 104</code></li>
</ul>
<p>Hard，统计交换后的最大差，计算交换部分数的最大和</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">maximumsSplicedArray</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums1, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums2)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>, sum1 = <span class="number">0</span>, sum2 = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i: nums1) sum1 += i;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i: nums2) sum2 += i;</span><br><span class="line"></span><br><span class="line">        ans = max(sum1, sum2);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> curr = <span class="number">0</span>, maxDiff = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums1.size(); i++) &#123;</span><br><span class="line">            curr += (nums2[i] - nums1[i]);</span><br><span class="line">            maxDiff = max(maxDiff, curr);</span><br><span class="line">            <span class="keyword">if</span> (curr &lt; <span class="number">0</span>) curr = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        ans = max(ans, max(sum1 + maxDiff, sum2 - maxDiff));</span><br><span class="line"></span><br><span class="line">        curr = <span class="number">0</span>;</span><br><span class="line">        maxDiff = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums1.size(); i++) &#123;</span><br><span class="line">            curr += (nums1[i] - nums2[i]);</span><br><span class="line">            maxDiff = max(maxDiff, curr);</span><br><span class="line">            <span class="keyword">if</span> (curr &lt; <span class="number">0</span>) curr = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        ans = max(ans, max(sum1 - maxDiff, sum2 + maxDiff));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2322-Minimum-Score-After-Removals-on-a-Tree"><a href="#Failed-2322-Minimum-Score-After-Removals-on-a-Tree" class="headerlink" title="Failed: 2322. Minimum Score After Removals on a Tree"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/minimum-score-after-removals-on-a-tree/">2322. Minimum Score After Removals on a Tree</a></h3><p>There is an undirected connected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> where <code>nums[i]</code> represents the value of the <code>ith</code> node. You are also given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [ai, bi]</code> indicates that there is an edge between nodes <code>ai</code> and <code>bi</code> in the tree.</p>
<p>Remove two <strong>distinct</strong> edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:</p>
<ol>
<li>Get the XOR of all the values of the nodes for <strong>each</strong> of the three components respectively.</li>
<li>The <strong>difference</strong> between the <strong>largest</strong> XOR value and the <strong>smallest</strong> XOR value is the <strong>score</strong> of the pair.</li>
</ol>
<ul>
<li>For example, say the three components have the node values: <code>[4,5,7]</code>, <code>[1,9]</code>, and <code>[3,3,3]</code>. The three XOR values are <code>4 ^ 5 ^ 7 = **6**</code>, <code>1 ^ 9 = **8**</code>, and <code>3 ^ 3 ^ 3 = **3**</code>. The largest XOR value is <code>8</code> and the smallest XOR value is <code>3</code>. The score is then <code>8 - 3 = 5</code>.</li>
</ul>
<p>Return <em>the <strong>minimum</strong> score of any possible pair of edge removals on the given tree</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705085752.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [1,5,5,4,11], edges &#x3D; [[0,1],[1,2],[1,3],[3,4]]</span><br><span class="line">Output: 9</span><br><span class="line">Explanation: The diagram above shows a way to make a pair of removals.</span><br><span class="line">- The 1st component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 &#x3D; 10.</span><br><span class="line">- The 2nd component has node [0] with value [1]. Its XOR value is 1 &#x3D; 1.</span><br><span class="line">- The 3rd component has node [2] with value [5]. Its XOR value is 5 &#x3D; 5.</span><br><span class="line">The score is the difference between the largest and smallest XOR value which is 10 - 1 &#x3D; 9.</span><br><span class="line">It can be shown that no other pair of removals will obtain a smaller score than 9.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705085814.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Input: nums &#x3D; [5,5,2,4,4,2], edges &#x3D; [[0,1],[1,2],[5,2],[4,3],[1,3]]</span><br><span class="line">Output: 0</span><br><span class="line">Explanation: The diagram above shows a way to make a pair of removals.</span><br><span class="line">- The 1st component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 &#x3D; 0.</span><br><span class="line">- The 2nd component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 &#x3D; 0.</span><br><span class="line">- The 3rd component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 &#x3D; 0.</span><br><span class="line">The score is the difference between the largest and smallest XOR value which is 0 - 0 &#x3D; 0.</span><br><span class="line">We cannot obtain a smaller score than 0.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 108</code></li>
<li><code>edges.length == n - 1</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= ai, bi &lt; n</code></li>
<li><code>ai != bi</code></li>
<li><code>edges</code> represents a valid tree.</li>
</ul>
<p>Hard，暂时没搞懂</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span><span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt; visited;</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; pc, adj; <span class="comment">// pc is the parent child edge in our dfs</span></span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt;&gt; childs; <span class="comment">// 2D array to store that i is a parent of j</span></span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; child_xor, nums, par; <span class="comment">// child_xor to store result of xors of a child node and par is a gloable array to track the parents of a node in dfs</span></span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">dfs</span><span class="params">(<span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> ans = nums[i];</span><br><span class="line">        visited[i] = <span class="literal">true</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> p: par) childs[p][i] = <span class="literal">true</span>; <span class="comment">// Defining this node as the child of all its parents</span></span><br><span class="line">		</span><br><span class="line">        par.push_back(i);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> child: adj[i])</span><br><span class="line">            <span class="keyword">if</span> (!visited[child]) &#123;</span><br><span class="line">                pc.push_back(&#123;i, child&#125;);</span><br><span class="line">                ans ^= dfs(child); <span class="comment">// Recurcively calculating xors</span></span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">        par.pop_back();</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> child_xor[i] = ans;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">minimumScore</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&amp; nums, <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; edges)</span> </span>&#123;</span><br><span class="line">		<span class="comment">// Initialising gloable variables</span></span><br><span class="line">        <span class="keyword">int</span> n = nums.size(), ans = INT_MAX;</span><br><span class="line">        visited = <span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt;(n);</span><br><span class="line">        pc = <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;();</span><br><span class="line">        adj = <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;(n);</span><br><span class="line">        child_xor = <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(n, <span class="number">0</span>);</span><br><span class="line">        childs = <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt;&gt;(n, <span class="built_in">vector</span>&lt;<span class="keyword">bool</span>&gt;(n, <span class="literal">false</span>));</span><br><span class="line">        <span class="keyword">this</span>-&gt;nums = nums;</span><br><span class="line">		par = <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;();</span><br><span class="line">		</span><br><span class="line">		<span class="comment">// Creating an adjacency matrix</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; &amp;edge: edges) adj[edge[<span class="number">0</span>]].push_back(edge[<span class="number">1</span>]), adj[edge[<span class="number">1</span>]].push_back(edge[<span class="number">0</span>]);</span><br><span class="line">		</span><br><span class="line">        dfs(<span class="number">0</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pc.size(); i++)</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; pc.size(); j++) &#123; <span class="comment">// removing an edge i and j</span></span><br><span class="line">                <span class="keyword">int</span> a = pc[i][<span class="number">1</span>], b = pc[j][<span class="number">1</span>]; <span class="comment">// node that will come below when you delete an edge i and j</span></span><br><span class="line">                <span class="keyword">int</span> xa = child_xor[a], xb, xc = child_xor[<span class="number">0</span>];</span><br><span class="line">                </span><br><span class="line">                <span class="keyword">if</span> (childs[a][b]) &#123;</span><br><span class="line">                    xb = child_xor[b];</span><br><span class="line">                    xc ^= xa;</span><br><span class="line">                    xa ^= xb;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    xb = child_xor[b];</span><br><span class="line">                    xc ^= xa;</span><br><span class="line">                    xc ^= xb;</span><br><span class="line">                &#125;</span><br><span class="line">                </span><br><span class="line">                ans = min(max(xa, max(xb, xc)) - min(xa, min(xb, xc)), ans);</span><br><span class="line">            &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> ans;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h2 id="Weekly-Contest-300"><a href="#Weekly-Contest-300" class="headerlink" title="Weekly Contest 300"></a><a target="_blank" rel="noopener" href="https://leetcode.com/contest/weekly-contest-300/">Weekly Contest 300</a></h2><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220705090111.png" alt="image-20220705090111656"></p>
<h3 id="2325-Decode-the-Message"><a href="#2325-Decode-the-Message" class="headerlink" title="2325. Decode the Message"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/decode-the-message/">2325. Decode the Message</a></h3><p>You are given the strings <code>key</code> and <code>message</code>, which represent a cipher key and a secret message, respectively. The steps to decode <code>message</code> are as follows:</p>
<ol>
<li>Use the <strong>first</strong> appearance of all 26 lowercase English letters in <code>key</code> as the <strong>order</strong> of the substitution table.</li>
<li>Align the substitution table with the regular English alphabet.</li>
<li>Each letter in <code>message</code> is then <strong>substituted</strong> using the table.</li>
<li>Spaces <code>&#39; &#39;</code> are transformed to themselves.</li>
</ol>
<ul>
<li>For example, given <code>key = &quot;**hap**p**y** **bo**y&quot;</code> (actual key would have <strong>at least one</strong> instance of each letter in the alphabet), we have the partial substitution table of (<code>&#39;h&#39; -&gt; &#39;a&#39;</code>, <code>&#39;a&#39; -&gt; &#39;b&#39;</code>, <code>&#39;p&#39; -&gt; &#39;c&#39;</code>, <code>&#39;y&#39; -&gt; &#39;d&#39;</code>, <code>&#39;b&#39; -&gt; &#39;e&#39;</code>, <code>&#39;o&#39; -&gt; &#39;f&#39;</code>).</li>
</ul>
<p>Return <em>the decoded message</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706083908.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: key &#x3D; &quot;the quick brown fox jumps over the lazy dog&quot;, message &#x3D; &quot;vkbs bs t suepuv&quot;</span><br><span class="line">Output: &quot;this is a secret&quot;</span><br><span class="line">Explanation: The diagram above shows the substitution table.</span><br><span class="line">It is obtained by taking the first appearance of each letter in &quot;the quick brown fox jumps over the lazy dog&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706083914.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: key &#x3D; &quot;eljuxhpwnyrdgtqkviszcfmabo&quot;, message &#x3D; &quot;zwx hnfx lqantp mnoeius ycgk vcnjrdb&quot;</span><br><span class="line">Output: &quot;the five boxing wizards jump quickly&quot;</span><br><span class="line">Explanation: The diagram above shows the substitution table.</span><br><span class="line">It is obtained by taking the first appearance of each letter in &quot;eljuxhpwnyrdgtqkviszcfmabo&quot;.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>26 &lt;= key.length &lt;= 2000</code></li>
<li><code>key</code> consists of lowercase English letters and <code>&#39; &#39;</code>.</li>
<li><code>key</code> contains every letter in the English alphabet (<code>&#39;a&#39;</code> to <code>&#39;z&#39;</code>) <strong>at least once</strong>.</li>
<li><code>1 &lt;= message.length &lt;= 2000</code></li>
<li><code>message</code> consists of lowercase English letters and <code>&#39; &#39;</code>.</li>
</ul>
<p>Easy，直接哈希保存数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="built_in">string</span> <span class="title">decodeMessage</span><span class="params">(<span class="built_in">string</span> key, <span class="built_in">string</span> message)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">code</span> <span class="params">(<span class="number">26</span>, <span class="number">0</span>)</span></span>;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: key) &#123;</span><br><span class="line">            <span class="keyword">if</span> (c != <span class="string">&#x27; &#x27;</span> &amp;&amp; code[c - <span class="string">&#x27;a&#x27;</span>] == <span class="number">0</span>) &#123;</span><br><span class="line">                code[c - <span class="string">&#x27;a&#x27;</span>] = i;</span><br><span class="line">                i++;</span><br><span class="line">                <span class="comment">// cout&lt;&lt;c&lt;&lt;&#x27;,&#x27;&lt;&lt;i&lt;&lt;endl;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">string</span> res = <span class="string">&quot;&quot;</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">char</span> c: message) &#123;</span><br><span class="line">            <span class="keyword">if</span> (c != <span class="string">&#x27; &#x27;</span>) &#123;</span><br><span class="line">                res += (<span class="keyword">char</span>)(code[c - <span class="string">&#x27;a&#x27;</span>] - <span class="number">1</span> + <span class="string">&#x27;a&#x27;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                res += c;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2326-Spiral-Matrix-IV"><a href="#2326-Spiral-Matrix-IV" class="headerlink" title="2326. Spiral Matrix IV"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/spiral-matrix-iv/">2326. Spiral Matrix IV</a></h3><p>You are given two integers <code>m</code> and <code>n</code>, which represent the dimensions of a matrix.</p>
<p>You are also given the <code>head</code> of a linked list of integers.</p>
<p>Generate an <code>m x n</code> matrix that contains the integers in the linked list presented in <strong>spiral</strong> order <strong>(clockwise)</strong>, starting from the <strong>top-left</strong> of the matrix. If there are remaining empty spaces, fill them with <code>-1</code>.</p>
<p>Return <em>the generated matrix</em>.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706084510.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: m &#x3D; 3, n &#x3D; 5, head &#x3D; [3,0,2,6,8,1,7,9,4,2,5,5,0]</span><br><span class="line">Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]</span><br><span class="line">Explanation: The diagram above shows how the values are printed in the matrix.</span><br><span class="line">Note that the remaining spaces in the matrix are filled with -1.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706084538.jpeg" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Input: m &#x3D; 1, n &#x3D; 4, head &#x3D; [0,1,2]</span><br><span class="line">Output: [[0,1,2,-1]]</span><br><span class="line">Explanation: The diagram above shows how the values are printed from left to right in the matrix.</span><br><span class="line">The last space in the matrix is set to -1.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m, n &lt;= 105</code></li>
<li><code>1 &lt;= m * n &lt;= 105</code></li>
<li>The number of nodes in the list is in the range <code>[1, m * n]</code>.</li>
<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
</ul>
<p>Medium，按照逻辑旋转坐标，写入链表中的数据</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * struct ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode *next;</span></span><br><span class="line"><span class="comment"> *     ListNode() : val(0), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x) : val(x), next(nullptr) &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int x, ListNode *next) : val(x), next(next) &#123;&#125;</span></span><br><span class="line"><span class="comment"> * &#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; spiralMatrix(<span class="keyword">int</span> m, <span class="keyword">int</span> n, ListNode* head) &#123;</span><br><span class="line">        <span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt; res(m, <span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;(n, <span class="number">-1</span>));</span><br><span class="line">        <span class="keyword">int</span> top = <span class="number">0</span>, bottom = m - <span class="number">1</span>, left = <span class="number">0</span>, right = n - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span> (head != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = left; j &lt;= right &amp;&amp; head != <span class="literal">nullptr</span>; j++) &#123;</span><br><span class="line">                res[top][j] = head-&gt;val;</span><br><span class="line">                head = head-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line">            top++;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = top; i &lt;= bottom &amp;&amp; head != <span class="literal">nullptr</span>; i++) &#123;</span><br><span class="line">                res[i][right] = head-&gt;val;</span><br><span class="line">                head = head-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line">            right--;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = right; j &gt;= left &amp;&amp; head != <span class="literal">nullptr</span>; j--) &#123;</span><br><span class="line">                res[bottom][j] = head-&gt;val;</span><br><span class="line">                head = head-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line">            bottom--;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = bottom; i &gt;= top &amp;&amp; head != <span class="literal">nullptr</span>; i--) &#123;</span><br><span class="line">                res[i][left] = head-&gt;val;</span><br><span class="line">                head = head-&gt;next;</span><br><span class="line">            &#125;</span><br><span class="line">            left++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="2327-Number-of-People-Aware-of-a-Secret"><a href="#2327-Number-of-People-Aware-of-a-Secret" class="headerlink" title="2327. Number of People Aware of a Secret"></a><a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-people-aware-of-a-secret/">2327. Number of People Aware of a Secret</a></h3><p>On day <code>1</code>, one person discovers a secret.</p>
<p>You are given an integer <code>delay</code>, which means that each person will <strong>share</strong> the secret with a new person <strong>every day</strong>, starting from <code>delay</code> days after discovering the secret. You are also given an integer <code>forget</code>, which means that each person will <strong>forget</strong> the secret <code>forget</code> days after discovering it. A person <strong>cannot</strong> share the secret on the same day they forgot it, or on any day afterwards.</p>
<p>Given an integer <code>n</code>, return <em>the number of people who know the secret at the end of day</em> <code>n</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p><strong>Example 1:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 6, delay &#x3D; 2, forget &#x3D; 4</span><br><span class="line">Output: 5</span><br><span class="line">Explanation:</span><br><span class="line">Day 1: Suppose the first person is named A. (1 person)</span><br><span class="line">Day 2: A is the only person who knows the secret. (1 person)</span><br><span class="line">Day 3: A shares the secret with a new person, B. (2 people)</span><br><span class="line">Day 4: A shares the secret with a new person, C. (3 people)</span><br><span class="line">Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)</span><br><span class="line">Day 6: B shares the secret with E, and C shares the secret with F. (5 people)</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: n &#x3D; 4, delay &#x3D; 1, forget &#x3D; 3</span><br><span class="line">Output: 6</span><br><span class="line">Explanation:</span><br><span class="line">Day 1: The first person is named A. (1 person)</span><br><span class="line">Day 2: A shares the secret with B. (2 people)</span><br><span class="line">Day 3: A and B share the secret with 2 new people, C and D. (4 people)</span><br><span class="line">Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= delay &lt; forget &lt;= n</code></li>
</ul>
<p>Medium，维护一个每天存货天数的数组</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">peopleAwareOfSecret</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> delay, <span class="keyword">int</span> forget)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; <span class="title">dp</span><span class="params">(forget, <span class="number">0</span>)</span></span>;</span><br><span class="line">        dp[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">            <span class="keyword">int</span> cur = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = forget - <span class="number">1</span>; j &gt; <span class="number">0</span>; j--) &#123;</span><br><span class="line">                dp[j] = dp[j - <span class="number">1</span>];</span><br><span class="line">                <span class="keyword">if</span> (j &gt;= delay) cur = (cur + dp[j]) % mod;</span><br><span class="line">            &#125;</span><br><span class="line">            dp[<span class="number">0</span>] = cur % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; forget; j++) &#123;</span><br><span class="line">            res = (res + dp[j]) % mod;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>时间复杂度过高，维护每天知道sercet人数的数组</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">peopleAwareOfSecret</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> delay, <span class="keyword">int</span> forget)</span> </span>&#123;</span><br><span class="line">        <span class="function"><span class="built_in">vector</span>&lt;<span class="keyword">long</span>&gt; <span class="title">dp</span><span class="params">(n + <span class="number">1</span>)</span></span>;</span><br><span class="line">        dp[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> share = <span class="number">0</span>, mod = <span class="number">1e9</span> + <span class="number">7</span>, res = <span class="number">0</span> ;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">2</span>; i &lt;= n; ++i)</span><br><span class="line">            dp[i] = share = (share + dp[max(i - delay, <span class="number">0</span>)] - dp[max(i - forget, <span class="number">0</span>)] + mod) % mod;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - forget + <span class="number">1</span>; i &lt;= n; ++i)</span><br><span class="line">            res = (res + dp[i]) % mod;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="Failed-2328-Number-of-Increasing-Paths-in-a-Grid"><a href="#Failed-2328-Number-of-Increasing-Paths-in-a-Grid" class="headerlink" title="Failed: 2328. Number of Increasing Paths in a Grid"></a>Failed: <a target="_blank" rel="noopener" href="https://leetcode.com/problems/number-of-increasing-paths-in-a-grid/">2328. Number of Increasing Paths in a Grid</a></h3><p>You are given an <code>m x n</code> integer matrix <code>grid</code>, where you can move from a cell to any adjacent cell in all <code>4</code> directions.</p>
<p>Return <em>the number of <strong>strictly</strong> <strong>increasing</strong> paths in the grid such that you can start from <strong>any</strong> cell and end at <strong>any</strong> cell.</em> Since the answer may be very large, return it <strong>modulo</strong> <code>109 + 7</code>.</p>
<p>Two paths are considered different if they do not have exactly the same sequence of visited cells.</p>
<p><strong>Example 1:</strong></p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20220706085534.png" alt="img"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[1,1],[3,4]]</span><br><span class="line">Output: 8</span><br><span class="line">Explanation: The strictly increasing paths are:</span><br><span class="line">- Paths with length 1: [1], [1], [3], [4].</span><br><span class="line">- Paths with length 2: [1 -&gt; 3], [1 -&gt; 4], [3 -&gt; 4].</span><br><span class="line">- Paths with length 3: [1 -&gt; 3 -&gt; 4].</span><br><span class="line">The total number of paths is 4 + 3 + 1 &#x3D; 8.</span><br></pre></td></tr></table></figure>

<p><strong>Example 2:</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Input: grid &#x3D; [[1],[2]]</span><br><span class="line">Output: 3</span><br><span class="line">Explanation: The strictly increasing paths are:</span><br><span class="line">- Paths with length 1: [1], [2].</span><br><span class="line">- Paths with length 2: [1 -&gt; 2].</span><br><span class="line">The total number of paths is 2 + 1 &#x3D; 3.</span><br></pre></td></tr></table></figure>

<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 1000</code></li>
<li><code>1 &lt;= m * n &lt;= 105</code></li>
<li><code>1 &lt;= grid[i][j] &lt;= 105</code></li>
</ul>
<p>Hard，记忆化dfs</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> &#123;</span></span><br><span class="line">    <span class="keyword">int</span> mod = <span class="number">1e9</span> + <span class="number">7</span>;</span><br><span class="line">    <span class="keyword">int</span> dirs[<span class="number">5</span>] = &#123;<span class="number">0</span>, <span class="number">-1</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>&#125;;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="keyword">int</span> <span class="title">countPaths</span><span class="params">(<span class="built_in">vector</span>&lt;<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt;&gt;&amp; grid)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> m = grid.size(), n = grid[<span class="number">0</span>].size();</span><br><span class="line">        <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> f[m][n];</span><br><span class="line">        <span class="built_in">memset</span>(f, <span class="number">-1</span>, <span class="keyword">sizeof</span>(f));</span><br><span class="line">        function&lt;<span class="keyword">int</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)&gt; dfs = [&amp;](<span class="keyword">int</span> i, <span class="keyword">int</span> j) -&gt; <span class="keyword">int</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (f[i][j] != <span class="number">-1</span>) <span class="keyword">return</span> f[i][j];</span><br><span class="line">            <span class="keyword">int</span> res = <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = <span class="number">0</span>; k &lt; <span class="number">4</span>; k++) &#123;</span><br><span class="line">                <span class="keyword">int</span> x = i + dirs[k], y = j + dirs[k + <span class="number">1</span>];</span><br><span class="line">                <span class="keyword">if</span> (<span class="number">0</span> &lt;= x &amp;&amp; x &lt; m &amp;&amp; <span class="number">0</span> &lt;= y &amp;&amp; y &lt; n &amp;&amp; grid[x][y] &gt; grid[i][j]) &#123;</span><br><span class="line">                    res = (res + (dfs(x, y))) % mod;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> f[i][j] = res;</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; m; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; n; j++) &#123;</span><br><span class="line">                res = (res + dfs(i, j)) % mod;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%88%B7%E9%A2%98/" rel="tag"><i class="fa fa-tag"></i> 刷题</a>
              <a href="/tags/%E5%8A%9B%E6%89%A3/" rel="tag"><i class="fa fa-tag"></i> 力扣</a>
              <a href="/tags/%E5%91%A8%E8%B5%9B/" rel="tag"><i class="fa fa-tag"></i> 周赛</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/04/23/GRID%20GARDEN%E4%B8%AA%E4%BA%BA%E7%AD%94%E6%A1%88/" rel="prev" title="GRID GARDEN">
      <i class="fa fa-chevron-left"></i> GRID GARDEN
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/06/12/%E8%87%AA%E5%B7%B1%E7%9A%84%E7%BC%BA%E7%82%B9%E6%80%9D%E8%80%83/" rel="next" title="自己的缺点思考">
      自己的缺点思考 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-283"><span class="nav-text">Weekly Contest 283</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2194-Cells-in-a-Range-on-an-Excel-Sheet"><span class="nav-text">2194. Cells in a Range on an Excel Sheet</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2195-Append-K-Integers-With-Minimal-Sum"><span class="nav-text">2195. Append K Integers With Minimal Sum</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2196-Create-Binary-Tree-From-Descriptions"><span class="nav-text">Failed: 2196. Create Binary Tree From Descriptions</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2197-Replace-Non-Coprime-Numbers-in-Array"><span class="nav-text">2197. Replace Non-Coprime Numbers in Array</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-284"><span class="nav-text">Weekly Contest 284</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2200-Find-All-K-Distant-Indices-in-an-Array"><span class="nav-text">2200. Find All K-Distant Indices in an Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2201-Count-Artifacts-That-Can-Be-Extracted"><span class="nav-text">2201. Count Artifacts That Can Be Extracted</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2202-Maximize-the-Topmost-Element-After-K-Moves"><span class="nav-text">Failed: 2202. Maximize the Topmost Element After K Moves</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2203-Minimum-Weighted-Subgraph-With-the-Required-Paths"><span class="nav-text">Failed: 2203. Minimum Weighted Subgraph With the Required Paths</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-285"><span class="nav-text">Weekly Contest 285</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2210-Count-Hills-and-Valleys-in-an-Array"><span class="nav-text">2210. Count Hills and Valleys in an Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2211-Count-Collisions-on-a-Road"><span class="nav-text">Failed: 2211. Count Collisions on a Road</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2212-Maximum-Points-in-an-Archery-Competition"><span class="nav-text">Failed: 2212. Maximum Points in an Archery Competition</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2213-Longest-Substring-of-One-Repeating-Character"><span class="nav-text">Failed: 2213. Longest Substring of One Repeating Character</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-286"><span class="nav-text">Weekly Contest 286</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2215-Find-the-Difference-of-Two-Arrays"><span class="nav-text">2215. Find the Difference of Two Arrays</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2216-Minimum-Deletions-to-Make-Array-Beautiful"><span class="nav-text">2216. Minimum Deletions to Make Array Beautiful</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2217-Find-Palindrome-With-Fixed-Length"><span class="nav-text">Failed: 2217. Find Palindrome With Fixed Length</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2218-Maximum-Value-of-K-Coins-From-Piles"><span class="nav-text">Failed: 2218. Maximum Value of K Coins From Piles</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-287"><span class="nav-text">Weekly Contest 287</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2224-Minimum-Number-of-Operations-to-Convert-Time"><span class="nav-text">Failed: 2224. Minimum Number of Operations to Convert Time</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2225-Find-Players-With-Zero-or-One-Losses"><span class="nav-text">2225. Find Players With Zero or One Losses</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2226-Find-Players-With-Zero-or-One-Losses"><span class="nav-text">2226. Find Players With Zero or One Losses</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2227-Encrypt-and-Decrypt-Strings"><span class="nav-text">Failed: 2227. Encrypt and Decrypt Strings</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-288"><span class="nav-text">Weekly Contest 288</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2231-Largest-Number-After-Digit-Swaps-by-Parity"><span class="nav-text">2231. Largest Number After Digit Swaps by Parity</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2232-Minimize-Result-by-Adding-Parentheses-to-Expression"><span class="nav-text">Failed: 2232. Minimize Result by Adding Parentheses to Expression</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2233-Maximum-Product-After-K-Increments"><span class="nav-text">2233. Maximum Product After K Increments</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2234-Maximum-Total-Beauty-of-the-Gardens"><span class="nav-text">Failed: 2234. Maximum Total Beauty of the Gardens</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-289"><span class="nav-text">Weekly Contest 289</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2243-Calculate-Digit-Sum-of-a-String"><span class="nav-text">2243. Calculate Digit Sum of a String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2244-Minimum-Rounds-to-Complete-All-Tasks"><span class="nav-text">2244. Minimum Rounds to Complete All Tasks</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2245-Maximum-Trailing-Zeros-in-a-Cornered-Path"><span class="nav-text">2245. Maximum Trailing Zeros in a Cornered Path</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2246-Longest-Path-With-Different-Adjacent-Characters"><span class="nav-text">Failed: 2246. Longest Path With Different Adjacent Characters</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-291"><span class="nav-text">Weekly Contest 291</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2259-Remove-Digit-From-Number-to-Maximize-Result"><span class="nav-text">2259. Remove Digit From Number to Maximize Result</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2260-Minimum-Consecutive-Cards-to-Pick-Up"><span class="nav-text">2260. Minimum Consecutive Cards to Pick Up</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2261-K-Divisible-Elements-Subarrays"><span class="nav-text">2261. K Divisible Elements Subarrays</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2262-Total-Appeal-of-A-String"><span class="nav-text">Failed: 2262. Total Appeal of A String</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-292"><span class="nav-text">Weekly Contest 292</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2264-Largest-3-Same-Digit-Number-in-String"><span class="nav-text">2264. Largest 3-Same-Digit Number in String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2265-Count-Nodes-Equal-to-Average-of-Subtree"><span class="nav-text">2265. Count Nodes Equal to Average of Subtree</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2266-Count-Number-of-Texts"><span class="nav-text">2266. Count Number of Texts</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2267-Check-if-There-Is-a-Valid-Parentheses-String-Path"><span class="nav-text">Failed: 2267. Check if There Is a Valid Parentheses String Path</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-293"><span class="nav-text">Weekly Contest 293</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2273-Find-Resultant-Array-After-Removing-Anagrams"><span class="nav-text">2273. Find Resultant Array After Removing Anagrams</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2274-Maximum-Consecutive-Floors-Without-Special-Floors"><span class="nav-text">2274. Maximum Consecutive Floors Without Special Floors</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2275-Largest-Combination-With-Bitwise-AND-Greater-Than-Zero"><span class="nav-text">2275. Largest Combination With Bitwise AND Greater Than Zero</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2276-Count-Integers-in-Intervals"><span class="nav-text">2276. Count Integers in Intervals</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-294"><span class="nav-text">Weekly Contest 294</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2278-Percentage-of-Letter-in-String"><span class="nav-text">2278. Percentage of Letter in String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2279-Maximum-Bags-With-Full-Capacity-of-Rocks"><span class="nav-text">2279. Maximum Bags With Full Capacity of Rocks</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2280-Minimum-Lines-to-Represent-a-Line-Chart"><span class="nav-text">2280. Minimum Lines to Represent a Line Chart</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2281-Sum-of-Total-Strength-of-Wizards"><span class="nav-text">Failed: 2281. Sum of Total Strength of Wizards</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-295"><span class="nav-text">Weekly Contest 295</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2287-Rearrange-Characters-to-Make-Target-String"><span class="nav-text">2287. Rearrange Characters to Make Target String</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2288-Apply-Discount-to-Prices"><span class="nav-text">2288. Apply Discount to Prices</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2289-Steps-to-Make-Array-Non-decreasing"><span class="nav-text">Failed: 2289. Steps to Make Array Non-decreasing</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2290-Minimum-Obstacle-Removal-to-Reach-Corner"><span class="nav-text">Failed: 2290. Minimum Obstacle Removal to Reach Corner</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-296"><span class="nav-text">Weekly Contest 296</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2293-Min-Max-Game"><span class="nav-text">2293. Min Max Game</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2294-Partition-Array-Such-That-Maximum-Difference-Is-K"><span class="nav-text">2294. Partition Array Such That Maximum Difference Is K</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2295-Replace-Elements-in-an-Array"><span class="nav-text">2295. Replace Elements in an Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2296-Design-a-Text-Editor"><span class="nav-text">Failed: 2296. Design a Text Editor</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-297"><span class="nav-text">Weekly Contest 297</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2303-Calculate-Amount-Paid-in-Taxes"><span class="nav-text">2303. Calculate Amount Paid in Taxes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2304-Minimum-Path-Cost-in-a-Grid"><span class="nav-text">2304. Minimum Path Cost in a Grid</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2305-Fair-Distribution-of-Cookies"><span class="nav-text">2305. Fair Distribution of Cookies</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2306-Naming-a-Company"><span class="nav-text">Failed: 2306. Naming a Company</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-298"><span class="nav-text">Weekly Contest 298</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2309-Greatest-English-Letter-in-Upper-and-Lower-Case"><span class="nav-text">2309. Greatest English Letter in Upper and Lower Case</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2310-Sum-of-Numbers-With-Units-Digit-K"><span class="nav-text">2310. Sum of Numbers With Units Digit K</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2311-Longest-Binary-Subsequence-Less-Than-or-Equal-to-K"><span class="nav-text">Failed: 2311. Longest Binary Subsequence Less Than or Equal to K</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2312-Selling-Pieces-of-Wood"><span class="nav-text">Failed: 2312. Selling Pieces of Wood</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-299"><span class="nav-text">Weekly Contest 299</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2319-Check-if-Matrix-Is-X-Matrix"><span class="nav-text">2319. Check if Matrix Is X-Matrix</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2320-Count-Number-of-Ways-to-Place-Houses"><span class="nav-text">2320. Count Number of Ways to Place Houses</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2321-Maximum-Score-Of-Spliced-Array"><span class="nav-text">Failed: 2321. Maximum Score Of Spliced Array</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2322-Minimum-Score-After-Removals-on-a-Tree"><span class="nav-text">Failed: 2322. Minimum Score After Removals on a Tree</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Weekly-Contest-300"><span class="nav-text">Weekly Contest 300</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2325-Decode-the-Message"><span class="nav-text">2325. Decode the Message</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2326-Spiral-Matrix-IV"><span class="nav-text">2326. Spiral Matrix IV</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2327-Number-of-People-Aware-of-a-Secret"><span class="nav-text">2327. Number of People Aware of a Secret</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Failed-2328-Number-of-Increasing-Paths-in-a-Grid"><span class="nav-text">Failed: 2328. Number of Increasing Paths in a Grid</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
